Monday, April 18, 2005

Simple Basic Concept in C#

It is always better to understand these concepts with an example rather than trying to understand paragraphs of explanation.

Example:
class sharpTest
{
static void Main()
{
string s = "Sunil";
string t = string.Copy(s);
Console.WriteLine(s == t);
Console.WriteLine((object)s == (object)t);
}
}

Output:
True
False

Inference:
Two expressions of type object are considered equal if both refer to the same object, or if both are null.
Two expressions of type string are considered equal if the string instances have identical lengths and identical characters in each character position, or if both are null.

Type conversions are similar to C++. Implicit conversions like int to long are done without any loss of data. Explicit conversions are done with a cast expression.

Arrays in C#
As usual, single-dimensional and multi-dimensional arrays are supported.

// Creates a single-dimensional array of 5 elements
int[] arr = new int[5];

What is to be noted is that arrays are also reference types and hence have to be explicitly allocate the memory by specifying the size of the array.

// Some more arrays
int[,,] a3; // 3-dimensional array of int
int[][] j2; // "jagged" array: array of (array of int)
int[][][] j3; // array of (array of (array of int))


Type system unification
class Test
{
static void Main() {
Console.WriteLine(3.ToString());
}
}
All types including value types are derived from the type object. It is possible to call object methods on any value, even values of “primitive” types such as int.

1 comment:

Mitesh V. Mehta said...

Hi
i tried accessing ur site, but found not available. Its give page not found.

Can u reconfirm the URL, please.

Mitesh