Monday 28 November 2011

7. Attributes


Attributes enable the programmer to give certain declarative information to the elements in their class. These elements include the class itself, the methods, the fields, and the properties. You can choose to use some of the useful built-in attributes provided with the .NET platform, or you can create your own. Attributes are specified in square brackets ( [. . .] ) before the class element upon which they’re implemented. Table 6 shows some useful attributes provided with .NET.

Table 6 Useful Built-in Attributes

NAME
DESCRIPTION
EXAMPLE
DllImport
Imports a native DLL
[DllImport(“winmm.dll”) ]
Serializable
Makes a class serializable
[Serializable]
Conditional
Includes/omits a method based on condition
[Conditional(Diagnostic”)]




 

8. Variables


A variable represents a strong location. Each variable has a type that determines what values can be stored in the variable. A variable must definitely be assigned before its value can be obtained.

In C#, you declare a variable in this format:

[modifiers] datatype identifier;

In this case, the modifier is an access modifier. The “variable Modifiers” section will discuss class member access modifiers. The data type refers to the type of value a variable can store. The identifier is the name of variable.

The next two examples are declarations of variable where public is the modifier, int is the data type, and num1 is the name. The second variable type is a local variable. A local variable can’t have modifier because it sits inside a method and is always private to the method. Here are the examples:

public int num1;

and:

int num1;

A value can be assigned to variable after it’s declared. You can also initialize a value during a variable declaration. For example:

int num1 = new Int16();
num1 = 34;
int num2 = 123;

Variable Modifiers


Modifiers enable you to specify a number of features that you apply to your variable. You apply a variable modifier when you declare a variable. Keep in mind that mo-differs can be applied to fields not to local variables.

Note: A local variable only has scope within its defined block in the program.

A variable can have one or combination of more then one of the following types: internal, new, private, public, protected, read only, and static.

Accessibility modifiers


Some of the modifiers discussed in previous sections can set the accessibility level of variables. These are called accessibility modifiers (see table 7).

Table 7. Accessibility modifiers

MODIFIER
DESCRIPTION
internal
The variable can only accessed by the current program.
public
The variable can be accessed from any where as a field.
protected
The variable can only be accessed with the class in which it’s defined and it’s derived class.
protected internal
The variable can only be accessed from the current program and the type derived from the current program.
private
The variable can only be accessed within the type in which it’s defined.



You’ll now examine access modifiers in an example. In listing 21, AccessCls is a class accessed by the Main method. The Main method has access to num1 because it’s defined as a public variable, but not to num2 because it’s a private variable.

Listing 21. Variable access modifiers.

using System;
class VarAccess
{
class AccessCls
{
public int num1 = 123;
int num2 = 54;
}
static void Main()
{
AccessCls cls = new AccessCls();
int num1 = 98;
num1 = cls.num1;
//int i = cls. Num2;
Console.WriteLine(num1.ToString());
}
}

When you access class members, the num2 variable is not available in the list of its members. See figure 6.


Figure 6. Available members of AccessCls

If you try access num2 from the main program, the compiler gives the error shown in figure 7


Figure 7. Error given when trying to access a private member of class

Static and Read-Only Variables


By default, a field is an instance field. That means a new copy of variable is creates for each instance of the class to which it belongs. There are some cases where you want the variable to be shared by ever instance of the class, and it’s in such cases that static fields are useful. By defining the static keyword, you can restrict a field to create only one instance of the variable of a class and share it with all other class instance of the same type. In other words, if you change the value of a static variable in a class, all instance at the class level rather then the instance level. You can use the static modifier alongside other modifiers.
For example:

public static int num2 = 34;

You can modify the value of a variable once it’s initialized, but there are some cases where you don’t want to change the value of the variable after it’s assigned during initialization. In these cases, you can the read –only modifier to prevent modification.

 

6. Types


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 3 describes simple types.

Table 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 9 shows some assignment examples.

Listing 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 10, your struct CarRec uses a record for a car with three members: name, model, and year.

Listing 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  5 shows the output of listing 10.


Figure 5. Output of listing 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 11 shows an properties, indexers, delegates, and events to the class. Listing 11 shows an example of a class type.

Listing 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 12 MyInterface is an interface that defines the method TestMethod.MyClass is derived from MyInterface, and you implement the MyMethod method in MyClass.

Listing 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 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 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 14 shows an example of delegate.

Listing 14. 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 15 shows an example of single- dimensional arrays.

Listing 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. Table4 lists and describes some of the array class properties.

Table 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 5 describes some of the array Class methods.

Table 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 16 shows a sample of coping array items from one array to another.

Listing 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 17 shows how to sort and reverse arrays.

Listing 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));

    }
}

Type Conversions


C# supports two kinds of type conversions: implicit conversions and explicit conversions. Some of the predefined types define predefined conversions, such as converting from an int type to a long type.

Implicit conversions are conversions in which one type can directly and safely are converted to another type. Generally, small range type converts to large range type. As an example, you’ll examine the process of converting from an int type to a long type. In this conversion, there is no loss of data, as shown in Listing 18.

Listing 18. Conversion example

using System;
class ConversionSamp
{
      static void Main()
{
int num1 = 123;
long num2 = num1;
Console.WriteLine(num1.ToString());
Console.WriteLine(num2.ToString());
}
}

Casting performs explicit conversions. There may be a chance of data loss or even some errors in explicit conversions. For example, converting a long value to an integer would result in data loss.

This is an example of an explicit conversion:

long num1 = Int64.MaxValue;
int num2 =(int)num1;
Console.WriteLine(num1.ToString());
Console.WriteLine(num2.ToString());

The process of converting from a value type to a reference type is called boxing. Boxing is an implicit conversion. Listing 19 shows an example of boxing.

Listing 19. Boxing example

using System;
class ConversionSamp
{
    static void Main()
    {
        int num1 = 123;
        Object obj = num1;

        Console.WriteLine(num1.ToString());
        Console.WriteLine(obj.ToString());
    }
}

The process of converting from a reference type to a value type is called unboxing. Listing 20 shows an example of unboxing.

Listing 20. Unboxing example

using System;
class ConversionSamp
{
    static void Main()
    {
        Object obj = 123;
        int num1 = (int)obj;

        Console.WriteLine(num1.ToString());
        Console.WriteLine(obj.ToString());
    }
}