C# - 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

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

}

}

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

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.

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.

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.

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,};

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.

HOME PREVIOUS NEXT