C# Workshop – Generics

Standard

Generics

C# contains many powerful features that are useful to allow the creation of software that can reuse or simplify many complex but common programming tasks, as projects become more complex there needs to be a better way to reuse code, to help with this C# includes a feature called Generics. Generics allows a class to take type parameters allow for more functionality in much the same way as Methods can be made more powerful because they take parameters. Generic Classes and Methods combine reusability and efficiency in a way that non-generic alternatives can’t, they are most frequently used with collections in fact List and Dictionary used previously are an example of Generics. When you encounter the < and > such as in List<int> the type within them can be any type which is what makes them Generic – that is they can be any type of List or Dictionary this type is usually indicated by using T and referred to as being type of T where T is the type.

To use Generics in C# enter in to dotnetfiddle.net the following:

using System;

namespace Workshop
{
	public class Container<T>
	{
		private T _value;
	
		public Container(T val)
		{
			this._value = val;
		}
	
		public T Get()
		{
			return this._value;
		}
	}
	
	public class Demo
	{
		public static void Main()
		{
			Container<string> name = 
				new Container<string>("John Smith");
			Console.WriteLine(name.Get());
		}
	}
}

In the example, there is a Container class which takes type of T denoted by which will be the type, then a value which is of type of T and a Get Method which returns type of T. Although this is a simple example the Container class could be used with any type.

To use another type in the previous example below the Console.WriteLine(name.Get()); line in dotnetfiddle.net enter the following:

Container<int> a = new Container<int>(4);
Container<int> b = new Container<int>(5);
Console.WriteLine(a.Get() + b.Get());

You can also use Generics to combine functionality needed such as creating a new instance of a class whatever its type might be using with a Factory class with a Create Method to create a new instance of the class.

To implement a basic Factory class in C#, enter in to dotnetfiddle.net the following:

using System;

namespace Workshop
{
	public class Factory<T> where T : new()
	{
		public T Create()
		{
			return new T();
		}
	}	
	
	public class Person
	{		
		public string Name { get; set; }
	}
	
	public class Demo
	{
		static Factory<Person> factory = new Factory<Person>();

		public static void Main()
		{
			Person person = factory.Create();
			person.Name = "John Smith";
			Console.WriteLine(person.Name);
		}
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s