Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.

Similar presentations


Presentation on theme: "Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five."— Presentation transcript:

1

2 Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five types of tokens Identifiers Keywords Literals Operators Punctuators

3 Identifiers That are programmer designed tokens (like variable name) They are used to naming classes, methods, variables, labels, namespaces etc.. Rules for Identifiers are as follows:- They can have alphabets, digits and underscore characters They must not begins with digit Uppercase and lowercase letters are distinct Keywords in standalone can not be use as identifier

4 Keywords That are the reserved words, and whose meaning is already explained to the C# compiler They can not be used as identifiers except when they are prefaced by @ character Some Examples of keywords are AbstractEventNamesp ace StaticAsExplicitNew StringBaseExternNullStructBoolFalse ObjectSwitchFinallyOperatorThisByteChar ForeachIfElseContinueDoEnumLong

5 Literals Literals are the way in which values that are stored in variables are represented So it is the value constants assigned to variables, or it is a result of expression in program C# Literals Numeric Booleancharacter String Single character Realinteger

6 Integer literals That is sequence of digit There are two type of integer Decimal (digit between 0 to 9 with optional minus sign) Hexadecimal (sequence preceded by 0x ) Spaces, commas and non-digit characters are not permitted between digits

7 Real Literals Number contains fractional part like 17.1234 are called real or floating point numbers Example of valid real literals are 9.002 -7.333 -0.34 215..95 -.71 Real literals also may expressed in exponential Mantissa e exponent 0.65e4

8 Boolean literals There are two Boolean literals values True False They are used as a values of relational expression

9 Characters Literals Single character literals That is a literal which contain single character enclosed within a pair of single quote marks. Eg: ‘2’, ‘a’, ‘ ’ String Literals: That contains sequence of characters Eg: “hi hello”, “02023u”

10 Backslash character Literals Some special constants that are used in output methods Constantmeaning ‘\a’Alert ‘\b’Back space ‘\n’New line ‘\r’Carriage return ‘\t’Horizontal tab ‘\v’Vertical tab ‘\’’Single Quote ‘\’”’Double Quote ‘\\’Backslash ‘\0’Null

11 Variable A variable is an identifier that is used to give name of storage location Constant remain unchanged during the program execution, A variable can change value during program execution Variable name can be chosen by programmer in a meaningful way, so as to reflect what it represent in program Variable name can be of any length

12 Data Type It specifies size and type of values can be stored Data types are divide in two categories: Value type Reference type These two types differ to each other by Where they are stored in memory How they behave in context of assignment statement Value type stored on stack, and when a value of variable assigned to another variable it actually copies the value, so two identical copies of the value are available in memory Reference type stored on heap of memory, when an assignment between two reference variable occurs, it copies only reference, actual value remains in same memory location

13 Type System Value types (user defined) Enums enum State { Off, On } Structs struct Point { int x, y; } Reference types (use defined) Classes class Foo: Bar, IFoo {...} Interfaces interface IFoo: IBar {...} Arrays string[] a = new string[10]; Delegates delegate void Empty();

14 Predefined Types C# predefined types Reference object, string Signed sbyte, short, int, long Unsigned byte, ushort, uint, ulong Character char Floating-point float, double, decimal Logical bool

15 nullable Type C# added a new type called nullable type This type variable can hold undefined value Any value type variable can be defined as a nullable type

16 Declaration and initialization of variable Declaration : int al float b; double pi; byte b; char c1,c2,c3; decimal d1,d2; uint m; ulong n; Variable name = Value; or Type variable name = value; or X=y=z=0;

17 Default Values TypeDefault Value All integer type0 Char type‘\x000’ Flaot type0.0f Double type0.0d Decimal type0.0 m Bool typeFalse Enum type0 All reference typenull

18 CONSTANT variable The variable whose value do not change during execution of program is known as constant variable const int a=10; Remember const int a; a=10; is illegal We do not use non constant value in expression int m=10; Const int n=m*5; //cause an error

19 Scope of Variables Scope mean is a region of code within which variable can be accessed C# defined several categories of variables Static Instance Array element Value parameters Output Parameters Local Variables

20 Class ABC { static int m; int n; void fun(int x,ref int y,out int z,int[] a) { int j=10; ………. } Static Variable Instance Variable X= Value Parameter Y = Reference Parameter Z= Output parameter a[0] = array element

21 Static and instance variable are known as field variable and scope begins at the place of declaration and ends when Main method terminates The variable x,y,z are parameters of method fun() X will exist till the end of the method Y and z do not create new storage location, instead, they represent the same storage location as the variable that are passed as argument The scope of variable is always same as underlying variable Array instance a[0] come in existence when an array instance is created, and exit when there are no references to that array instance Local variable can be used within that method only

22 BOXING and UNBOXING In OOP, methods are invoked using object Since int a, where a is a variable, can not be used to call method like a.fun() For that we need to convert that value type to Object So boxing mean conversion of a value type on stack to a object type on a heap Conversion of object type back to value type is known as unboxing

23 BOXING int m=100; Object obj=m; // create a box to hold m (BOXING) int m=10; object obj = m; m=20; Console.WriteLine(m); Console.WriteLine(obj);

24 UNBOXING int m=10; Object ob=m; //box m int n = (int)obj; //unboxing


Download ppt "Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five."

Similar presentations


Ads by Google