0% found this document useful (0 votes)
14 views59 pages

UNIT_2

Uploaded by

nrpcespce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views59 pages

UNIT_2

Uploaded by

nrpcespce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

UNIT - 2

 Name Spaces
 Constructor and Destructors
 Function Overloading & Inheritance
 Operator Overloading
 Modifiers - Property and Indexers
 Attributes & Reflection API
 When to use Console Applications
 Generating Console Output, Processing Console
Input
27-01-2016 Prepared by Mittal Rayjada 2
 C# is a modern, general-purpose, object-
oriented programming language developed by
Microsoft and approved by European
Computer Manufacturers Association
(ECMA) and International Standards
Organization (ISO).
 C# was developed by Anders Hejlsberg and
his team during the development of .Net
Framework.
27-01-2016 Prepared by Mittal Rayjada 3
 C# is designed for Common Language
Infrastructure (CLI), which consists of the
executable code and runtime environment that
allows use of various high-level languages on
different computer platforms and
architectures.
 C# is pronounced as CSharp.
 The extension of the csharp files are.cs

27-01-2016 Prepared by Mittal Rayjada 4


 he following reasons make C# a widely used
professional language:
 It is a modern, general-purpose programming language
 It is object oriented.
 It is easy to learn.
 It is a structured language.
 It produces efficient programs.
 It can be compiled on a variety of computer platforms.
 It is a part of .Net Framework.

27-01-2016 Prepared by Mittal Rayjada 5


 Microsoft provides the following development
tools for C# programming:
 Visual Studio 2010 (VS)
 Visual C# 2010 Express (VCE)
 Visual Web Developer

27-01-2016 Prepared by Mittal Rayjada 6


 Although the.NET Framework runs on the
Windows operating system, there are some
alternative versions that work on other
operating systems.
 Mono is an open-source version of the .NET
Framework which includes a C# compiler and
runs on several operating systems, including
various flavors of Linux and Mac OS.

27-01-2016 Prepared by Mittal Rayjada 7


 The stated purpose of Mono is not only to be
able to run Microsoft .NET applications cross-
platform, but also to bring better development
tools for Linux developers.
 Mono can be run on many operating systems
including Android, BSD, iOS, Linux, OS X,
Windows, Solaris, and UNIX.

27-01-2016 Prepared by Mittal Rayjada 8


 A C# program consists of the following parts:
 Namespace declaration
 A class
 Class methods
 Class attributes
 A Main method
 Statements and Expressions
 Comments

27-01-2016 Prepared by Mittal Rayjada 9


using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
27-01-2016 Prepared by Mittal Rayjada 10
 When this code is compiled and executed, it
produces the following result:

27-01-2016 Prepared by Mittal Rayjada 11


 The first line of the program using System; - the using
keyword is used to include the System namespace in
the program.
 The next line has the namespace declaration. A
namespace is a collection of classes. The
HelloWorldApplication namespace contains the class
HelloWorld.
 The next line has a class declaration, the class
HelloWorld contains the data and method definitions
that your program uses.
 Classes generally contain multiple methods.
 Methods define the behavior of the class.
 However, the HelloWorld class has only one method Main.
27-01-2016 Prepared by Mittal Rayjada 12
 The next line defines the Main method, which is the entry
point for all C# programs.
 The Main method states what the class does when executed.
 The next line /*...*/ is ignored by the compiler and it is put
to add comments in the program.
 The Main method specifies its behavior with the statement
Console.WriteLine("Hello World");
 WriteLine is a method of the Console class defined in the
System namespace. This statement causes the message
"Hello, World!" to be displayed on the screen.
 The last line Console.ReadKey(); is for the VS.NET Users.
This makes the program wait for a key press and it prevents
the screen from running and closing quickly when the
program is launched from Visual Studio .NET.
27-01-2016 Prepared by Mittal Rayjada 13
 C# is case sensitive.

 All statements and expression must end with a


semicolon (;).

 The program execution starts at the Main


method.

 Unlike Java, program file name could be


different from the class name.
27-01-2016 Prepared by Mittal Rayjada 14
 Start Visual Studio.
 On the menu bar, choose File -> New -> Project.
 Choose Visual C# from templates, and then
choose Windows.
 Choose Console Application.
 Specify a name for your project and click OK
button.
 This creates a new project in Solution Explorer.
 Write code in the Code Editor.
 Click the Run button or press F5 key to execute
the project. A Command Prompt window appears
that contains the line Hello World.
27-01-2016 Prepared by Mittal Rayjada 15
 Open a text editor and add the above-mentioned code.
 Save the file as helloworld.cs
 Open the command prompt tool and go to the directory where you
saved the file.
 Type csc helloworld.cs and press enter to compile your code.
 If there are no errors in your code, the command prompt takes you
to the next line and generates helloworld.exe executable file.
 Type helloworld to execute your program.
 You can see the output Hello World printed on the screen.

27-01-2016 Prepared by Mittal Rayjada 16


27-01-2016 Prepared by Mittal Rayjada 17
 The using Keyword
 The using keyword is used for including the
namespaces in the program. A program can include
multiple using statements.
 The class Keyword
 The class keyword is used for declaring a class.
 Comments in C#
 Comments are used for explaining code. Compilers
ignore the comment entries.
 The multiline comments in C# programs start with /*
and terminates with the characters */.
 The single line comments are indicated by “//” symbol.
27-01-2016 Prepared by Mittal Rayjada 18
 Member Variables
 Variables are attributes or data members of a class, used for
storing data. In the preceding program, the Rectangle class
has two member variables named length and width.
 Member Functions
 Functions are set of statements that perform a specific task.
The member functions of a class are declared within the
class. Our sample class Rectangle contains three member
functions: AcceptDetails, GetArea and Display.
 Instantiating a Class
 In the preceding program, the class ExecuteRectangle
contains the Main() method and instantiates the Rectangle
class.

27-01-2016 Prepared by Mittal Rayjada 19


 An identifier is a name used to identify a class, variable,
function, or any other user-defined item. The basic rules for
naming classes in C# are as follows:
 A name must begin with a letter that could be followed by a
sequence of letters, digits (0 - 9) or underscore. The first
character in an identifier cannot be a digit.
 It must not contain any embedded space or symbol such as?
- + ! @ # % ^ & * ( ) [ ] { } . ; : " ' / and \. However, an
underscore ( _ ) can be used.
 It should not be a C# keyword.

27-01-2016 Prepared by Mittal Rayjada 20


 Keywords are reserved words predefined to the
C# compiler. These keywords cannot be used as
identifiers. However, if you want to use these
keywords as identifiers, you may prefix the
keyword with the @ character.
 In C#, some identifiers have special meaning in
context of code, such as get and set are called
contextual keywords.
 The following table lists the reserved keywords
and contextual keywords in C#:
27-01-2016 Prepared by Mittal Rayjada 21
27-01-2016 Prepared by Mittal Rayjada 22
27-01-2016 Prepared by Mittal Rayjada 23
 Variables in C# are categorized into the
following:
VARIABLE

REFERENCE
VALUE TYPES PONTER TYPES
TYPE

27-01-2016 Prepared by Mittal Rayjada 24


 Value type variables can be assigned a value
directly. They are derived from the class
System.ValueType.
 The value types directly contain data.
 Some examples are int, char, and float, which
stores numbers, alphabets, and floating point
numbers, respectively.
 When you declare an int type, the system
allocates memory to store the value.
27-01-2016 Prepared by Mittal Rayjada 25
27-01-2016 Prepared by Mittal Rayjada 26
 To get the exact size of a type or a variable on
a particular platform, you can use the sizeof
method.
 The expression sizeof(type) yields the storage
size of the object or type in bytes.
 Following is an example to get the size of int
type on any machine:

27-01-2016 Prepared by Mittal Rayjada 27


 Output :

27-01-2016 Prepared by Mittal Rayjada 28


 The reference types do not contain the actual data stored in
a variable, but they contain a reference to the variables.
 In other words, they refer to a memory location. Using
multiple variables, the reference types can refer to a
memory location.
 If the data in the memory location is changed by one of the
variables, the other variable automatically reflects this
change in value.
 Example of built-in reference types are: object, dynamic,
and string.

27-01-2016 Prepared by Mittal Rayjada 29


 The Object Type is the ultimate base class for all
data types in C# Common Type System (CTS).
 The object types can be assigned values of any
other types, value types, reference types,
predefined or user-defined types.
 However, before assigning values, it needs type
conversion.
 When a value type is converted to object type, it
is called boxing and on the other hand, when an
object type is converted to a value type, it is
called unboxing.

27-01-2016 Prepared by Mittal Rayjada 30


 The String Type allows you to assign any
string values to a variable.
 The string type is an alias for the
System.String class.
 It is derived from object type. The value for a
string type can be assigned using string literals
in two forms: quoted and @quoted.
 For example,
 String str = "Tutorials Point";
27-01-2016 Prepared by Mittal Rayjada 31
TYPE CONVERSION

IMPLICIT EXPLICIT

27-01-2016 Prepared by Mittal Rayjada 32


 Implicit conversions are performed by C# in a
type-safe manner.
 For example, are conversions from smaller to
larger integral types and conversions from derived
classes to base classes.
 Explicit conversions are done explicitly by
users using the pre-defined functions.
 Explicit conversions require a cast operator.

27-01-2016 Prepared by Mittal Rayjada 33


27-01-2016 Prepared by Mittal Rayjada 34
27-01-2016 Prepared by Mittal Rayjada 35
27-01-2016 Prepared by Mittal Rayjada 36
27-01-2016 Prepared by Mittal Rayjada 37
27-01-2016 Prepared by Mittal Rayjada 38
27-01-2016 Prepared by Mittal Rayjada 39
 A variable is nothing but a name given to a
storage area that our programs can manipulate.
 Each variable in C# has a specific type, which
determines the size and layout of the variable's
memory the range of values that can be stored
within that memory and the set of operations that
can be applied to the variable.

 The basic value types provided in C# can be


categorized as:

27-01-2016 Prepared by Mittal Rayjada 40


27-01-2016 Prepared by Mittal Rayjada 41
 Syntax
 <data_type> <variable_name>;
 e.g.
 int i;
 Here, data_type must be a valid C# data type.
 variable_list may consist of one or more
identifier names separated by commas.
 E.g. int i,j,k;

27-01-2016 Prepared by Mittal Rayjada 42


 Syntax
 <data_type> <variable_name> = value;
OR

 <variable_name> =value
 E.g.
 int d = 3, f = 5;
 byte z = 22;
 double pi = 3.14159;
 char x = 'x';
27-01-2016 Prepared by Mittal Rayjada 43
27-01-2016 Prepared by Mittal Rayjada 44
 a=10,b=20,c=30

27-01-2016 Prepared by Mittal Rayjada 45


 The Console class in the System namespace
provides a function ReadLine() for accepting
input from the user and store it into a variable.
 For example,

 The function Convert.ToInt32() converts the


data entered by the user to int data type, because
Console.ReadLine() accepts the data in string
format.
27-01-2016 Prepared by Mittal Rayjada 46
 The constants refer to fixed values that the
program may not alter during its execution.
 These fixed values are also called literals.
 Constants can be of any of the basic data types
like an integer constant, a floating constant, a
character constant, or a string literal.
 There are also enumeration constants as well.
 The constants are treated just like regular
variables except that their values cannot be
modified after their definition.

27-01-2016 Prepared by Mittal Rayjada 47


 An integer literal can be a decimal, octal, or
hexadecimal constant.
 A prefix specifies the base or radix: 0x or 0X for
hexadecimal, 0 for octal, and no prefix id for
decimal.
 An integer literal can also have a suffix that is a
combination of U and L, for unsigned and long,
respectively.
 The suffix can be uppercase or lowercase and can
be in any order.

27-01-2016 Prepared by Mittal Rayjada 48


27-01-2016 Prepared by Mittal Rayjada 49
 A floating-point literal has an integer part, a
decimal point, a fractional part, and an
exponent part.
 You can represent floating point literals either
in decimal form or exponential form.

27-01-2016 Prepared by Mittal Rayjada 50


 Character literals are enclosed in single quotes.
 For example, 'x' and can be stored in a simple
variable of char type.
 A character literal can be a plain character (such
as 'x'), an escape sequence (such as '\t'), or a
universal character (such as '\u02C0').
 There are certain characters in C# when they are
preceded by a backslash.
 The have special meaning and they are used to
represent like newline (\n) or tab (\t).

27-01-2016 Prepared by Mittal Rayjada 51


27-01-2016 Prepared by Mittal Rayjada 52
 String literals or constants are enclosed in double
quotes "" or with @"".
 A string contains characters that are similar to
character literals:
 plain characters
 escape sequences
 universal characters.

 You can break a long line into multiple lines


using string literals and separating the parts using
whitespaces.

27-01-2016 Prepared by Mittal Rayjada 53


27-01-2016 Prepared by Mittal Rayjada 54
 Syntax
 const <data_type> <variable_name> = value;
KEYWORD

27-01-2016 Prepared by Mittal Rayjada 55


27-01-2016 Prepared by Mittal Rayjada 56
Enter Radius: 3
Radius: 3, Area: 28.27431

27-01-2016 Prepared by Mittal Rayjada 57


 An operator is a symbol that tells the compiler to
perform specific mathematical or logical
manipulations.
 C# has rich set of built-in operators and provides
the following type of operators:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators

27-01-2016 Prepared by Mittal Rayjada 58


27-01-2016 Prepared by Mittal Rayjada 59

You might also like