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 TypesValue 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 TypesSimple 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
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 TypeA 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 exampleusing 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-10Enum data typesThe 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 TypesA 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 1public 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 exampleusing 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 interfacesusing 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 TypesDelegate 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 TypesThe 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 exampleclass 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 declarationint [, ,]arr3= new int[2,4,6]; // also rectangularint[][]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 ArraysThe 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
Table 1-5 describes some of the array Class methods.Table 1-5. The array class methods
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 sampleusing 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 elementsusing 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)); }}