FREE BOOK

Chapter 1: Introduction to C#

Posted by Apress Free Book | C# Language December 08, 2008
This Chapter will introduce you to C#. You will learn how to write and compile C# programs, C# syntaxes, data types, control flow, classes and their members, inter-faces, arrays, and exception handling I'll begin with an overview of the language.

Type in C# 
 
As mentioned earlier in the article, C# supports value types and reference types. Value types include simple data type such as int, char, and bool. Reference types include object, class, interface, and delegate.
 
 A value type contains the actual value of the object. That means the actual data is stored in the variable of a value type, whereas a reference type variable contains the reference to the actual data.

Value Types

Value types reference the actual data and declared by using their default constructors. The default constructor of these types returns a zero- initialized instance of the variable. The value types can further be categorized instance of the variable. The value types can further be categorized into many subcategories, described in the following sections.

Simple Types

Simple types include basic data types such as int, char, and bool. These types have a reserved keyword corresponding to one class of a CLS type defined in the System class. For example, the keyword int aliases the System.Int32 type, and the keyword long aliases the System.Int64 type. Table 1-3 describes simple types.

Table 1-3 simple types

C# TYPE ALIAS CLS TYPE SIZE BITS SUFFIX DESCRIPTION RANGE
sbyte Sbyte 8 N/a Singed byte -128 to 127
byte Byte 8 N/a Unsigned byte 0 to 255
short Int16 16 N/a Short integer -32,768 to 32,767
ushort unit16 16 N/a Unsigned short integer 0 to 65,535
int Int32 32 N/a Integer -2,147,483,648 to 2,17483,648
uint uint32 32 U Unsigned integer 0 to 4,294,967,295
long Int64 64 L Long integer -9223372036854775808 to 9223372036854775808
ulong uint64 64 N/a Unsigned long integer 0 to 18,446,744,073,709,551,615
char char 16 N/a Unicode character any valid character, e.g., a,*, \x0058 (hex), or\u0058 (Unicode)
float single 32 F Floating point integer  
double double 64 D Double floating point integer  
bool boolean 1 N/a Logical true/false value True/false
decimal decimal 128 M Used for financial and monetary calculations  

One feature of simple types is that you can assign single direct values to these types. Listing 1-9 shows some assignment examples.

Listing 1-9 Simple type example

using System;
namespace ToStringSamp
{
    class Test
    {
        static void Main(string[] args)
        {
            int num1 = 12;
            float num2 = 3.05f;
            double num3 = 3.5;
            bool bl = true;
            Console.WriteLine(num1.ToString());
            Console.WriteLine(num2.ToString());
            Console.WriteLine(num3.ToString());
            Console.WriteLine(bl.ToString());
        }
    }
}

Struct Type

A struct type, or structure type, can declare constructors, constants, fields, methods, properties, indexers, operators, and nested types. Structure types are similar to classes, but they're lightweight objects with no inheritance mechanism.

However, all structures inherit from the Object class.

In listing 1-10, your struct CarRec uses a record for a car with three members: name, model, and year.

Listing 1-10. a struct type example

using System;
struct CarRec
{
    public string Name;
    public string Model;
    public int Year;
}
class TestStructureType
{
    public static void Main()
    {
        CarRec rec;
        rec.Name = "Honda";
        rec.Model = "Accord";
        rec.Year = 1999;
        Console.WriteLine("Car Name: " + rec.Name);
        Console.WriteLine("Car Modal: " + rec.Model);
        Console.WriteLine("Car: " + rec.Year);
    }
}

Figure 1- 5 shows the output of listing 1-10.



Figure 1-5. Output of listing 1-10

Enum data types

The enum data types are useful when you need to represent a set of multiple values. A good example of an enumeration is a list of colors:

enum ColorEnum {black, red, green};

Enum types are limited to long, int, short and byte.

This code declares an enum ColorEnum with members black, red, and green:

//black is 0, red is 1, green is 2.
enum ColorEnum{black, red, green};

You can also set your associated value to an e num type such as:

enum ColorEnum {black =0, red =1, green =2};

By default, enum associated value starts with 0 and increases by 1 for the next defined member. If you assign your value, the default value of the next e num type member will be the value of current member plus 1. For example, in this code the value of green is 7;

enum ColorEnum {black =0, red =6, green };

Reference Types

A reference type is a reference to an instance type. The main reference types are class, array, interface, delegate, and event. A null value is assigned to a reference type by default. A type assigned to a null value means the absence of an instance of that type.

Class Type

A class type defines a data structure that can have members in the form of methods, properties, indexers, events, constructors, operators, and delegates. The class keyword is used to create a class type. You can add methods, properties, indexers, delegates, and events to the class. Listing 1-11 shows an properties, indexers, delegates, and events to the class. Listing 1-11 shows an example of a class type.

Listing 1-11 Class Type example

// Define Class 1
public class class1 : Object
{
    private void Method1()
    {
        Console.WriteLine("1 method");
    }
}

The
new keyword creates access to the class type. After creating an instance, you can use the dot (.) operator to access its members, as shows here:

Class1 cls1 = new class1();
cls1.Method1();

I'll return to the discussion of classes later in this article.

Interface Type

An interface type is an abstract base class, which is a skeleton of a class and doesn't implement the members that it defines. Only the derived class of an interface can implement the members of the interface. Interfaces can contain methods, properties, events, and indexers.

In listing 1-12 MyInterface is an interface that defines the method TestMethod.MyClass is derived from MyInterface, and you implement the MyMethod method in MyClass.

Listing 1-12 the interface type example

using System;
interface MyInterface
{
    void TestMethod();
}
class MyClass : MyInterface
{
    public static void Main()
    {
        MyClass cls = new MyClass();
        cls.TestMethod();
    }
    public void TestMethod()
    {
        Console.WriteLine("Test Method");
    }
}

A class can also implement multiple interfaces. Listing 1-13 defines two interfaces, MyInterface and MyInterface2.MyClass is inherited from these interfaces. You must implement these interfaces in the inherited class. If you don't implement an interface in the derived class, the complier gives an error message.

For example, if you don't implement the method test method TestMethod2 of MyInterface2 in Myclass, the compiler returns this message: "Myclass does not implement the interface member 'MyInterface2. TestMethod2 (int, int)'."

Listing 1-13 Multiple interfaces

using System;
interface MyInterface
{
    void TestMethod();
}
interface MyInterface2
{
    int TestMethod2(int a, int b);
}
class MyClass : MyInterface, MyInterface2
{
    public static void main()
    {
        int num1 = 23;
        int num2 = 6;
        MyClass cls = new MyClass();
        cls.TestMethod();
        int tot = cls.TestMethod2(num1, num2);
        Console.WriteLine(tot.ToString());
    }
    public void TestMethod()
    {
        Console.WriteLine("test method");
    }
    public int TestMethod2(int a, int b)
    {
        return a + b;
    }
}

Delegates Types

Delegate types are mainly are used with the class events. A delegate type encapsulates a method with a certain signature, called a callable entity. Delegates are the typesafe and secure version of function pointers (callback functionality).

Delegate instances are not aware of the methods they encapsulate; they're aware only and return type.

There are three steps in defining and using a delegate: declaration syntax. For example, this code:

delegate void MyDelegate():

Declares a delegate named MyDelegate that no arguments and returns void.
The next step is to create an instance of delegate and call it:

MyDelegate del =new MyDelegate(TestMethod);

del();

Listing 1-14 shows an example of delegate.

Listing 1-14 shows an example of delegate.

delegate void MyDelegate();
class Test
{
    static void TestMethod()
    {
        System.Console.WriteLine("Test Method called");
    }
    static void Main()
    {
        MyDelegate del = new MyDelegate(TestMethod);
        del();
    }
}

Event Types

The event keyword defines an event. An eventype enables an object or class to provide notification of an event from the system. An instance of a delegate type encapsulates the callable entities. The EventHandler class defines a delegate definition. For example:

public delegate void EventHandler(object sender, System.Event Args e);
public event EventHandler Click;
...........

I'll discuss events in more detail in the "Class Members" section of this article.

Array Types

An array type is a sequential set of any of the other types. Arrays can be either single- or multidimensional. Both rectangular and jagged arrays are supported a jagged array has elements that don't necessarily have the same length. A rectangular array is multidimensional, and all of its subarrays have the same length. With arrays, all of the elements must be of the same base type. In C#, the lower index of an array starts with 0, and the upper index is number of item minus 1.

You can initialize array item either during the creation of an array or later by referencing array item, as shown here:

int[] nums = new int[5];
int[0] = 1;
int[1] = 2;
int[2] = 3;
int[3] = 4;
int[4] = 5;

Or here

int[] nums = new int {1,2,3,4,5,};

Listing 1-15 shows an example of single- dimensional arrays.

Listing 1-15. Single- dimensional array example

class Test
{
          static void Main()
          {
                   //array of integers
                   int[] nums = new int[5];
                   // Array of strings
                   string[ ] names = new string[2];
                   for(int i =0; i< nums.Length; i++)
                             nums[i] = i+2;
                   names[0] = "Mahesh";
                   names[1] = "Chand";
 for (int i = 0; i< nums.Length; i++)
System.Console.WriteLine ("num[{0}] = {1}", i, nums[i] );
System.Console.WriteLine
(names[0].ToString() + " " + names[1].ToString() );
}
}

The following is an example is an example of multiple, rectangular, and jagged arrays:

char[] arr1 =new char[] {'a', 'b', 'c'};
int[,] arrr2 = new int[,] {{2,4}, {3, 5}};
//rectangular array declaration
int [, ,]arr3= new int[2,4,6];
// also rectangular
int[][]jarr = new int[3][];
//jagged array declaration
jarr[0] = new int[] {1,2,3};
jarr[1] = new int[] {1,2,3,4,5,6};
jarr[2] = new int[] {1,2,3,4,5,6,7,8,9};

Sorting Searching, and Copying Arrays

The array class defines functionalities for creating, manipulating, searching, shorting, and copying arrays. Table1-4 lists and describes some of the array class properties.

Table 1-4. The array class properties

PROPERTY DESRIPITION
Length Number of items in an array
Rank Number of dimensions in an array
IsFixedLength Indicates if an array is of fixed length
IsReadOnly Indicates if an array is read-only

Table 1-5 describes some of the array Class methods.

Table 1-5. The array class methods
 

METHOD DESCRIPTION
BinarySearch Searches for an element using Binary search algorithm
Clear Removes all elements of an array and set reference to null
Copy Copies a section of one array to another
CreateInstance Initializes a new instance of an array
Reverse Reverses the order of array elements
Sort Sorts the elements of an array
Clone Creates a shallow copy of an array
CopyTo Copies all elements from 1 D array to another
GetLength Returns number of items in an array
GetValue Gets a value at a specified location
SetValue Sets a value at a specified location

The Copy method copies one-array section to another array section. However, this method only works for single-dimensional array. Listing 1-16 shows a sample of coping array items from one array to another.

Listing 1-16. Copying array sample

using System;
public class ArraySample
{
          public static void Main()
          {
                   // Create and initialize a new arrays
                   int[] intArr = new int[5] {1,2,3,4,5};
                   Object[] objArr = new Object[5] {10,20,30,40,50};
                   foreach (int i in intArr)
                   {
                             Console.Write(i);
                             Console.Write(",");
                   }
                   Console.WriteLine();
                   foreach (Object i in objArr )
                   {
                             Console.Write (i);
                             Console.Write (",");
                   }
                   Console.WriteLine();
                   // Copy one first 3 elements of intArr to objArr
                   Array.Copy(intArr, objArr,3);
                   Console.WriteLine("After coping" );
                   foreach (int i in intArr)
                   {
                             Console.Write(i);
                             Console.Write(" , ");
                   }
                   Console.WriteLine( );
                   foreach (Object i in objArr)
                   {
                             Console.Write(i);
                             Console.Write(" ,");
                   }
                   Console.WriteLine( );
          }
}

The Sort and Reverse methods of the array class are useful when you need to sort and reverse array elements. Listing 1-17 shows how to sort and reverse arrays.

Listing 1-17. Reversing and sorting array elements

using System;
public class ArraySample
{
    public static void Main()
    {
        // Create and initialize a new array instance.
        Array strArr = Array.CreateInstance(typeof(string), 3);
        strArr.SetValue("Mahesh", 0);
        strArr.SetValue("chand", 1);
        strArr.SetValue("Test Array", 2);
        // Display the values of the array.
        Console.WriteLine("Initial Array values:");
        for (int i = strArr.GetLowerBound(0);
i <= strArr.GetUpperBound(0); i++)
            Console.WriteLine(strArr.GetValue(i));
        //sort the value of the array.
        Array.Sort(strArr);
        Console.WriteLine("After sorting:");
        for (int i = strArr.GetLowerBound(0);
i <= strArr.GetUpperBound(0); i++)
            Console.WriteLine(strArr.GetValue(i));
        // Reverse values of the array.
        Array.Reverse(strArr);
        for (int i = strArr.GetLowerBound(0); i <= strArr.GetUpperBound(0); i++)
            Console.WriteLine(strArr.GetValue(i));
    }
}

Total Pages : 10 34567

comments