Innovation Through Knowledge

Main menu

What is C#.Net Interfaces

This C# Tutorial deals with interfaces in C# .Net. An Interface is a reference type and it contains only abstract members. Interface’s members can be Events, Methods, Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can’t contain constants, data fields, constructors, destructors and static members.

Interface Sample – C# Tutorial:

Let us look at a simple example for c# interfaces. In this example interface declare base functionality of node object.

interface INode
{

string Text
{
get;
set;
}

object Tag
{
get;
set;
}

int Height
{
get;
set;
}

int Width
{
get;
set;
}

float CalculateArea();

}

The following code defines some more get and set properties such as Text, Tag, Height, Width and a method CalculateArea. So, we only define some members of interface now we need to create a class that inherits base functionality of INode interface.
//Deriving a class using a C# .Net interface – c# tutorial

public class Node : INode
{

public Node()
{}

public string Text
{

get
{
return m_text;
}

set
{
m_text = value;
}

}

private string m_text;
public object Tag
{

get
{
return m_tag;
}

set
{
m_tag = value;
}

}

private object m_tag = null;
public int Height
{

get
{
return m_height;
}

set
{
m_height = value;
}

}

private int m_height = 0;
public int Width
{

get
{
return m_width;
}

set
{
m_width = value;
}

}

private int m_width = 0;

public float CalculateArea()
{

if((m_width<0)||(m_height<0))
return 0;

return m_height*m_width;

}

}

Now the above code has created a c# class Node that inherits from INode c# interface and implement all its members. A very important point to be remembered about c# interfaces is, if some interface is inherited, the program must implement all its declared members. Otherwise the c# compiler throws an error.

The above code was a simple example of c# interface usage. Now this has to be followed with some advanced details of interface building in C# .Net. The previous example used only names of methods or properties that have the same names as in interface. But there is another alternative method for writing the implementation for the members in class. It uses full method or property name e.g. INode.CalculateArea () {// implemetation}.

Multiple Inheritance using C# interfaces – C# Tutorial:

Next feature that obviously needs to be explained is multiple inheritance using c# interfaces. This can be done using child class that inherits from any amount of c# interfaces. The inheritance can also happen with a combination of a C# .Net class and c# interfaces. Now let us see a small piece of code that demonstrate us multiple inheritance using only interfaces as parent data types.

class ClonableNode : INode,ICloneable
{

public object Clone()
{
return null;
}

// INode members
}

The above example created a class ClonableNode. It implements all the functionality of INode interface in the same way as it was done in Node class. Also it realizes Clone method – only one item of IClonable interface of .NET library.

is Operator for C# .Net interfaces – C# Tutorial:

At last a new C# operator that can be used to define that class should be explained. It is the “is” operator. Please have a look at the following piece of code:

if(nodeC is INode)
Console.WriteLine(“nodeC is object of INode type”);
else
Console.WriteLine(“nodeC isn’t object of INode type”);
In example nodeC – object that was created as ClonableNode type, but when we run program “if operator” returns true. It means that nodeC also has INode type. Ultimately the main objective of the usage of C# .Net interfaces is to divide the definition from implementation.

8 C# .Net Tutorial Multithreading

Any Windows application must have one or more processes. A Process is structural unit with a memory block and using some set of resources. For each executable, the Windows operating system creates some isolated memory block. This C# .Net Tutorial tries to explain the basics of Multithreading in C# .Net.

Every process must have at least one thread. The first thread is created with a process and is known as primary thread. This Primary Thread is entry point of application. In traditional Windows applications it is the method WinMain() and in console applications it is named main().

Main goal of creating multithreading application is performance improvement. As an example, imagine a situation where in a user starts a long process (e.g. copying), he can’t use a single threaded application and wait for an infinite time for the operation to get completed. But if he uses multi–threading application he can set copying process in the background and interact with application without any problems.

At first, if one wants to create a multi-threaded application an important point to be remembered is, a global variable, which is being accessed by different threads, can try to modify the same variable. This is a generic problem, which is solved using a mechanism called Synchronization of threads. Synchronization is nothing but the process of creating some set of rules to operate data or resources.

The C# .Net language has a powerful namespace which can be used for programming with Threads as well as Thread Synchronization in C# .Net programming. The name of the namespace is Sytem.Threading. The most important class inside this namespace for manipulating the threads is the C# .Net class Thread. It can run other thread in our application process.

Sample program on C# Multithreading – C# Tutorial:

The example it creates an additional C# .Net class Launcher. It has only one method, which output countdown in the console.

//Sample for C# tutorial on Multithreading using lock

public void Coundown()
{

lock(this)
{

for(int i=4;i>=0;i–)
{

Console.WriteLine(“{0} seconds to start”,i);

}

Console.WriteLine(“GO!!!!!”);

}

}

There is a new keyword lock inside the above chunk of .Net C# tutorial code. This provides a mechanism for synchronizing the thread operation. It means at the same point of time only one thread can access to this method of created object. Unless the lock is released after completion of the code, the next routine or iteration cannot enter the block.

To understand it more clearly please have a look at the piece of main method’s code:

As you see there were created three additional threads. These threads start a method of object that has Launcher type. The above program is a very simple example of using multi-threading in C#. Net. But C# .Net allows us to create more powerful applications with any level of complexity.