Class, interface, delegates, dynamic, object, string in C#

Reference types store the reference, unlike the value type. The value types store the value directly. The reference variables store the address of where the value resides. There may be more than one variable pointing to the same address.

The following types in C# are used to declare a reference type in C#.

1. Class

We refer to this type with a ‘class’ keyword. C# allows single inheritance on a class and multiple interfaces can be implemented. We can also implement many interfaces and single inheritance. A class is declared in the namespace and it is internal by default. I will discuss the internal modifier later. Let me give you an example of how one can declare a class in C#.

class Polygon
{
	private int height;
	private int width; 

	public Polygon()
	{
	height = 0;
	width = 0;
	}
}

Above example, The class is defined as shown in the example. There are two private variables called height and width.

2. Interface

Interface is like a contract which declares a set of functions that will be supported. The class will implement the interface and implements all the functions. The contract will give a flexibility about the implementation except that the interface agreement binds on the return type, arguments and function name.

interface IShape
{
    float area(int side1, int side2);
}
class Polygon : IShape
{
    private int height;
    private int width;

    public Polygon()
    {
        height = 0;
        width = 0;
    }
    public float area(int s1, int s2)
    {
        float area;
        area = s1 * s2;
        return area;
    }
}

3. Delegate

You may have heard of function pointers in C++. These are the function pointers in C#.

The use cases of delegates are

  • when the user wants to pass a function as parameters
  • call back functions or event handlers.

So you may be wondering how can we use a delegate. Each delegate needs to be declared, set a method, and then invoke the delegate. I call this a 3-step action. A delegate is declared as shown in the image.

I will explain this in detail with a separate post later and provide a link to it here.

Built-in reference types

1. Dynamic

A dynamic type is type that will skip the compile time type checking and will leave it to be resolved at run-time.

static void DynamicTutorial()
{
    dynamic number = 1.78;
    Console.WriteLine(number.GetType());
}

************
Output
System.Double

In the example, I declared a variable called number. But I did not declare its type. I keep it as dynamic and let the program decide its type at run-time. Wen I call this from my main program it prints ‘System. Double’ as its type.

2. Object

C# refers to an Object as something containing values that belong to any type. By saying any type, I mean it can be a user-defined type, reference type, or a value type.

When we convert a value from any type to an object type is called Boxing. When we convert an object to a particular type it is called unboxing. This is a popular interview question in C#.

//BOXING
int i = 0;
object o = i; 

//UNBOXING
int j = (int) o;

3. String

The string is a sequence of characters that contain zero or more Unicode characters.

I will give you an example of string usage.

static void stringTutorial()
{
    string firstWord = " Good";
    string secondWord = "Day";
    string greetings = firstWord + secondWord;
    Console.WriteLine(greetings);
}
**************
OUTPUT
 GoodDay

We can concatenate strings or we can refer to it as an array of characters or we can trim the whitespaces etc. ‘System.String’ allows us to perform a lot of operations that can make a developer’s life easy.

Conclusion

Now we understand what these words in C# mean. Each topic needs a separate post which I will introduce later. I hope you enjoyed this article.

Have fun learning.

Subscribe to Telestreak here- for ebooks.

Prev: Enum, Structure and Nullable types

Next: Coming soon