GitHub Proposal – Default Generic Type

While providing the same generic type several times for a generic class, it occurred to me, what if there was a default generic type in C#? It would save the hassle of providing the same type. At the same, it would provide the flexibility to use another type. This can improve the productivity of the developer/code reviewer, maintaining flexibility.

I have provided that idea on the GitHub. It is as follows:

Problem:
A generic class can have the same generic type used multiple times. Allow setting default generic type to reduce redundant code and making it easier to read.
Current:
public class Node<T>
{
	public T Data
	{
	   get;
	   set;
	}
}

Use:

Node<int> node1 = new Node<int>() { Data = 1};
Node<int> node2 = new Node<int>() { Data = 2};
Node<string> nodeStr = new Node<string>() { Data = "test" };

Proposed: note: T = int in the declaration

public class Node<T = int>
{
	public T Data
	{
	   get;
	   set;
	}
}

Use:

Node<> node1 = new Node<>() { Data = 1};
Node<> node2 = new Node<>() { Data = 2};
Node<string> nodeStr = new Node<string>() { Data = "test" };
Please provide feedback on the GitHub
Alternatively, you can provide feedback here. I invite you to discuss it.
Advertisements

Published by: Sameer Khandekar

This blog is about my hobby projects. The hobby projects include Arduino, IOT, ElectricImp, Mobile Apps and anything that catches interest. The intention of the blog is to share the knowledge. To know more about me: https://sameerkapps.wordpress.com/about/

Categories UncategorizedLeave a comment

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