• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
C# chap 4
 

C# chap 4

on

  • 35 views

 

Statistics

Views

Total Views
35
Views on SlideShare
35
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    C# chap 4 C# chap 4 Document Transcript

    • Chapter 4 Literals, variables and Data Types The smallest, non-reducible, textual elements in a program are referred to as Tokens. A C# program is a collection of tokens, comments and white spaces. C# include the following five types of tokens: Keywords, Identifiers, Literals, Operators, Punctuators Keywords are an essential part of a language definition. They implement specific features of the language. They are reserved, and cannot be used as identifiers except when they are prefaced by the @ character. Identifiers are programmer –designed tokens. They are used for naming classes, methods, variables, labels, namespace, interfaces, etc. C# identifiers enforce the following rules: They can have alphabets, digits and underscore character. They must not begin with a digit. Upper case and lower case letters are distinct. Literals are the way in which the values that are stored in variables are represented. Operators are symbols used in expressions to describe operations involving one or more operands. Punctuators are symbols used for grouping and separating code. Punctuators in C# include: Parentheses, Braces, Brackets, Semicolon, Colon, Comma, Period. C# implements several types of statements: Empty Statements, Labeled Statement, Declaration statements, Expression statement, Selection statement, Interaction statements, Jump statements, try statements, checked statements, unchecked statements, lock statements, using statements. Scope of Variables The scope of variables is the region of code within which the variable can be accessed. C# variables are categorized into - Static variables - Instance Variables - Array elements - Value Parameters - Reference Parameters - Output Parameters - Local variables
    • the scope of variables are - m is a static variable - n is an instance variable - a is a value parameter - b is an reference parameter - c is an output parameter - arr is an array element -d is a local variable. Static and instance variables are declared at the class level and are known as fields and ends when the main method terminates. The reference and output parameters do not create new storage locations, they represent the same storage locations as the variables that are passed as arguments. The scope of a local variables starts immediately after its identifier in the declaration and extends up to the end of the block containing the declaration. C# makes a fundamental distinction between the field variables declared at the class level and the local variables declared within methods. Boxing and Unboxing Boxing means the conversion of a value type on the stack to a object type on the heap, conversely, the conversion from an object type back to a value type is known as unboxing. Boxing – Any type, value or reference can be assigned to an object without an explicit conversion. When the compiler finds a value type where it needs a reference type, it creates an object ‘Box’ into which it places the value of the value type. Unboxing – Unboxing is the process of converting the object type back to the value type. A variable that has previously been boxed can only be unboxed. While unboxing the object could be cast to any type. class Sample { static int m; int n; void Test (int a, ref int b, out int c, int [] arr) { int d = 100; ………… } }
    • Method Overloading - C# allows us to create more than one method with the same name, but with the different parameter lists and different definitions. This is called method overloading. - Method overloading is used when methods are required to perform similar tasks but using different input parameters. - Overloaded methods must differ in number and/or type of parameters they take. This enables the compiler to decide which one of the definitions to execute depending on the type and number of arguments in the method call. - using the concept of method overloading, a family of methods with one name but different arguments lists can be designed. The method selection involves the following steps: 1) The compiler tries to find an exact match in which the types of actual parameters are same and uses that method. 2) if the exact match is not found, then the compiler tries to use the implicit conversions to the actual arguments and then uses the method whose match is unique. If the conversion creates multiple matches, then the compiler will generate an error message.