There are different modifiers which decide the visibility of the types or members.

They are:

Modifier

Applies To Description
public Any types or members The item is visible to any other code.
protected Any member of a type, also any nested type The item is visible only to any derived type.
internal Any member of a type, also any nested type The item is visible only within its containing assembly.
private Any types or members The item is visible only inside the type to which it belongs.
protected internal Any member of a type, also any nested type The item is visible to any code within its containing assembly and also to any code inside a derived type.

Some other modifiers are:

Modifier

Applies To Description
public Any types or members The item is visible to any other code.
protected Any member of a type, also any nested type The item is visible only to any derived type.
internal Any member of a type, also any nested type The item is visible only within its containing assembly.
private Any types or members The item is visible only inside the type to which it belongs.
protected internal Any member of a type, also any nested type The item is visible to any code within its containing assembly and also to any code inside a derived type.

To compile files other than .exe we have to use different compiler options…. for that we have to use /t or /target  switch… for eg to compile a library /t:library example.cs the compiler will compile this into a dll…

The exhaustive list of compiler options can be seen here.

XML Documentation

C# is the only language which provides us the facility for xml documentation. We can tell the compiler about the xml documentation while compiling with the /doc tag.

To make xml documentation possible we have to comment using /// instead of //.

All the tags and info regd. the documentation is available here.

Casing

There are two types of casings which are recommended for use in C#. They are PascalCasing and camelCasing.

Camel casing is recommended for the following names:

  • For names of all private member fields in types.
  • For names of all parameters passed to methods.
  • To distinguish items that would otherwise have the same name. A common example is when a property wraps around a field.

Pascal casing is used for remaining names.

  • Value types store the value and copying it another variable makes two copies of that whereas in reference types the 2 variables will be referencing to the same value so changes made to one variable will also reflect in other.

  • Value types are allocated on stack and reference types on managed heap.

  • Decimal datatype is not implemented as primitive type so using it eill have performance impact.

  • C# char is of 2 bytes as compared to C++ char which is of 1 byte.

  • strings can be assigned using ‘=’ ign but they are reference types.when I copy one string to another variable they both refer to the same string but when modified, another copy is created and both variables contain different strings.This happens because of “operator overloading”.

  • Enumerations are of integer type. They are instantiated as ’structs’, so i can run many methods over my enums and there is no performance loss. Once compliled the enums exist as primitive types just like int or float.

  • I can obtain the string value of enum for eg. TimeOfDay time
    time = TimeOfDay.Afternooh; writeline(Afternoon.ToString())

  • Also i can obtain the enum value from the string
    TimeOfDay time2 = (TimeOfDay)Enum.Parse(typeof(TimeOfDay),Afternoon,true);writeline((int)time2)…op will be int value of Afternoon.

  • Arrays syntax int[] var = new int[5]; Arrays are reference types.Zero based indexing is followed.

  • Setting Aliases to the namespaces using alias  = namespace; to instantiate a class use the namespace alias qualifier :: eg.alias::classname

  • If the program contains more than one Main(). I can tell the compiler to use a specific main by using /main switch , together with full name of the class including the namespace to wich the main belongs. eg, csc Hello.cs /main:namespace.class

  • Parameters can be passedto the Main method…then the Main syntax will be  public static int Main(string[] args)

  • to compile the file to a class library from a console window i can use the /target switch to tell which type to be compiled to.eg. /t:exe to a console app, /t:library, to a classlibrary.

  • /out can be used to mention the output file name. To reference any class library in console app use /r sritch. eg asa hello.cs /r:Math.dll.

  • Preprocessor statements help in debugging…#define…#undef, tell the compiler that a symbol exists and only if the smbol exists then…#if symbol…

If…then…else

If then else is a condtional statement where if condition is true then then clause is executed or else clause is executed

if(condition)
{
     //statements;
}
else
    //statements;
}

In C#, the expression in the if clause must evaluate to a Boolean.

Switch

The switch…case statement is good for selecting one branch of execution from a set of mutually exclusive ones.When the expression in the switch argument evaluates to one of the values beside a case clause, the code immediately following the case clause executes. This is one example where you don’t need to use curly braces to join statements into blocks; instead, you mark the end of the code for each case using the break statement. You can also include a default case in the switch statement, which will execute if the expression evaluates to none of the other cases.the case values must be constant expressions; variables are not permitted.

if a case clause is fired early on in the block, later clauses cannot be fired unless you use a goto statement to mark that you want them fired, too. it means the break statement is mandatory…

There is one exception to the no-fall-through rule, however, in that you can fall through from one case to the next if that case is empty. This allows you to treat two or more cases in an identical way (without the need for goto statements).

The order of the cases doesn’t matter.

The For Loop

The C# for loop is for iterating a set of statements for a set of times depending on the condition.

 (initializer; condition; iterator)
    statement(s)

where

  • The initializer is the expression evaluated before the first loop is executed (usually initializing a local variable as a loop counter).

  • The condition is the expression checked before each new iteration of the loop (this must evaluate to true for another iteration to be performed).

  • The iterator is an expression evaluated after each iteration (usually incrementing the loop counter). The iterations end when the condition evaluates to false.

The while Loop

The while loop is identical to the while loop in C++ and Java, and the While…Wend loop in Visual Basic. Like the for loop, while is a pretest loop. The syntax is similar, but while loops take only one expression:

 while(condition)   statement(s);
 

Unlike the for loop, the while loop is most often used to repeat a statement or a block of statements for a number of times that is not known before the loop begins.

The do . . . while Loop

The do…while loop is the post-test version of the while loop. It does the same thing with the same syntax as do…while in C++ and Java, and the same thing as Loop…While in Visual Basic. This means that the loop’s test condition is evaluated after the body of the loop has been executed. Consequently, do…while loops are useful for situations in which a block of statements must be executed at least one time.

bool condition;
do
{
   // This loop will at least execute once, even if Condition is false.
   MustBeCalledAtLeastOnce();
   condition = CheckCondition();
} while (condition);

 

The foreach Loop

The foreach loop allows you to iterate through each item in a collection.

foreach (int temp in arrayOfInts)
{
   Console.WriteLine(temp);
}

The goto Statement

The goto statement allows you to jump directly to another specified line in the program, indicated by a label (this is just an identifier followed by a colon):

 goto Label1;    Console.WriteLine("The control doesn't reavh here...");
Label1:    Console.WriteLine("instead control strts executing frm here...");

Some restrictions are there for using goto statement..

  • You can’t jump into a block of code such as a for loop.

  • you can’t jump out of a class, and you can’t exit a finally block after try…catch blocks

There is one place where it is quite handy: jumping between cases in a switch statement, particularly because C#’s switch is so strict on fall-through.

The break Statement

When you use the break statement the control reaches the statement immediately after the end of the loop…If the statement occurs in a nested loop, control will switch to the end of the innermost loop.

The continue Statement

The continue statement is similar to break, and must also be used within a for, foreach, while, or do…while loop. However, it exits only from the current iteration of the loop, meaning that execution will restart at the beginning of the next iteration of the loop, rather than outside the loop altogether.

The return Statement

The return statement is used to exit a method of a class, returning control to the caller of the method. If the method has a return type, return must return a value of this type; otherwise if the method returns void, you should use return without an expression.

C# implements string as a class which encapsulates many a functionaltity which makes life much easier.

String is a reference type…if you equate 2 string objects they point to the same value… but the catch here is tht if you change any one then it will create a complete new object leaving the other unchanged.

String literals are enclosed in double quotation marks (“…”);

Because these escape sequences start with a backslash, you can’t use this character unescaped in a string. Instead, you need to escape it with two backslashes (\\):          string filepath = “C:\\Windows\\example.cs”;

You can prefix a string literal with the at character (@) and all the characters in it will be treated at face value; they won’t be interpreted as escape sequences:

string filepath = @”C:\Windows\example.cs”;

Will Update later…

 All the types are ultimately derieved from System.Object. The main advantages of this are (a) you can set the returntype of any method as Object if you are not sure of its type and (b) The Object type implement some basic methods like Equals(), GetHashCode(), ToString()…. which you can use as they are or override them to suit your functionality…

Value Types

  • Value types w.r.t C# are nothing but Simple types such as int,float,double…
  • Value types store the value directly.
  • they are stored in an area called Stack

for eg:  int p=14;
              int q=p;
The above statements result in 2different 14s in two different locations

The different integer types as in msdn are:

Type Range Size
sbyte -128 to 127 Signed 8-bit integer
byte 0 to 255 Unsigned 8-bit integer
char U+0000 to U+ffff Unicode 16-bit character
short -32,768 to 32,767 Signed 16-bit integer
ushort 0 to 65,535 Unsigned 16-bit integer
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer
uint 0 to 4,294,967,295 Unsigned 32-bit integer
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer
ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer

 The floating point types are:

Type Approximate range Precision
float ±1.5e−45 to ±3.4e38  32 – bit 7 digits
double ±5.0e−324 to ±1.7e308  64 – bit 15-16 digits
decimal ±1.0 × 10−28 to ±7.9 × 1028    128 – bit 28 – 29 digits

Reference Types

  • Reference types store the address of the memory where the value is stored.
  • they are stored in an area called Managed Heap.

for eg: myclass c1,c2; //myclass is a class which contains an int field p
             c1.p=14;// p is 14
c2 = c1;
c2.p=9;//now both c1 and c2 contains p as 9 since both were pointing to same memory location.

Some Important bits:

  • for storing hexadecimal values in integer type…the values are prefixed by ox.     long x = 0×12ab;
  • similarly for storing different types of integers letters are appended..                     uint ui = 1234U;
    long l = 1234L;
    ulong ul = 1234UL;
  • when saving float values the values are appended by F…  float f = 12.3F;

Escape sequences for c# are:

  • \' – single quote, needed for character literals
  • \" – double quote, needed for string literals
  • \\ – backslash
  • - Unicode character 0
  • \a – Alert (character 7)
  • \b – Backspace (character 8)
  • \f – Form feed (character 12)
  • \n – New line (character 10)
  • \r – Carriage return (character 13)
  • \t – Horizontal tab (character 9)
  • \v – Vertical quote (character 11)
  • \uxxxx – Unicode escape sequence for character with hex value xxxx
  • \xn[n][n][n] – Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx)
  • \Uxxxxxxxx – Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)

int i;

This statement declares an int named i. The compiler won’t actually let you use this variable until you have initialized it with a value, but the declaration allocates 4 bytes on the stack to hold the value.

If you declare and initialize more than one variable in a single statement, all of the variables will be of the same data type:

 int x = 10, y =20;   // x and y are both ints
int x = 10, bool y = true;   // This won't compile!

Don’t assign different data types within a multiple variable declaration.

The basic rule in C# for variables is that they should be initialised before using them in any form. Variables that are fields in a class or struct, if not initialized explicitly, are by default zeroed out when they are created.Variables that are local to a method must be explicitly initialized in your code prior to any statements in which their values are used. In this case, the initialization doesn’t have to happen when the variable is declared, but the compiler will check all possible paths through the method and will flag an error if it detects any possibility of the value of a local variable being used before it is initialized.

The same rules apply to reference types as well. Consider the following statement:

 OneClass objOneObject;

In C++, this line would create an instance of the OneClass class on the stack. In C#, this same line of code would only create a reference for a OneClass object, but this reference does not yet actually refer to any object. Any attempt to call a method or property against this variable would result in an error.

Instantiating a reference object in C# requires use of the new keyword. You create a reference as shown in the previous example and then point the reference at an object allocated on the heap using the new keyword:

 objOneObject = new OneClass();   // This creates a Something on the heap

Constants are the variables whose values donot change. Variables are designated as constants using const keyword in front of the variable defination.

const int i = 100;  

Constants in C# dont have all the power as C++ constants…in C++ you can define a constant and later have pointer to that i.e constant pointers , variable pointers to constants, constant methods…all these are discarded in C#.

There are advantages for using Constants in programming.

  • You can use readable names instead of hard reading numbers.
  • if you have to change the value…for eg you have a constant for number of months depicting 12 months…and later you want to change the number to 24 then all you have to do is change the value of the constant in its defination.