Monday, May 10, 2010

The very basic C#

A Simple C# Program
C# demands that all program logic be contained within a type definition. As explained earlier type is a general term referring to a class, interface, structure, enumeration or a delegate. In C# it is not possible to create global functions or global points of data, all data members and methods must be contained within a type definition.
A Sample C# program looks like,

Every executable C# application (console program, Windows desktop program, or Windows service) must contain a class defining a Main() method, which is used to signify the entry point of the application. The class that defines the Main() method is termed the application object. Note that the signature of Main() is decoreted with the static keyword, so it is scoped to the class level (rather than the object level) and can thus be invoked without the need to first create a new class instance. The method has a parameter, Main ( string[] args ) this parameter may contain any number of incoming command-line arguments.
Few Miscellaneous things
Strings Are Immutable
Once we assign a string object with its initial value, the character data cannot be changed. Also assigning a value to a string object results in a new string object.
But the StringBuilder in System.Text namespace modifying the object’s internal character data (and is thus more efficient), not obtaining a copy of the data in a modified format.
Type casting
eg:
byte myByte = 0;
int myInt = 200;
myByte = myInt;
When we wish to inform the compiler than we are willing to deal with a possible loss of data, we must apply an explicit cast using the C# casting operator () otherwise the following compiler error will occur
"Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)"
checked and unchecked keywords
To handle overflow or underflow conditions in the application, C# provides keywords checked and unchecked. The default behavior of the .NET runtime is to ignore arithmetic overflow. When selectively handle discrete statements, make use of the checked keyword. If you wish to trap overflow errors throughout your application, enable the /checked flag. Finally, the unchecked keyword may be used if we have a block of code where overflow is acceptable (and thus should not trigger a runtime exception).
When we wrap a statement (or a block of statements) within the scope of the checked keyword, the C# compiler emits additional CIL instructions that test for overflow conditions that may result when adding, multiplying, subtracting, or dividing two numerical data types. If an overflow has occurred, you will receive a runtime exception (System.OverflowException to be exact).

System.Convert provides a language-neutral manner to convert between data types.
The out Modifier
Output parameters must be assigned by the method being called (and therefore are passed by reference). If the called method fails to assign output parameters, you are issued a compiler error. it allows the caller to obtain multiple return values from a single method invocation.
The ref Modifier
The value is initially assigned by the caller and may be optionally reassigned by the called method (as the data is also passed by reference). No compiler error is generated if the called method fails to assign a ref parameter.
Output parameters do not need to be initialized before they passed to the method. The reason for this is the method must assign output parameters before exiting. Reference parameters must be initialized before they are passed to the method. The reason for this is we are passing a reference to an existing variable. If we don’t assign it to an initial value, that would be the equivalent of operating on an unassigned local variable.
The params Modifier
This parameter modifier allows to send in a variable number of arguments as a single logical parameter. A method can have only a single params modifier, and it must be the final parameter of the method.
eg:
double CalculateAverage(params double[] values)
{

}
double average;
average = CalculateAverage(4.0, 3.2, 5.7, 64.22, 87.2);
double[] data = { 4.0, 3.2, 5.7 };
average = CalculateAverage(data);
Array Manipulation in C#

No comments:

Post a Comment