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.

Published by: Sameer Khandekar

I am a passionate software engineer who loves to work on Azure microservices, REST API, SDKs, and .NET apps using WPF, Xamarin, and MAUI. The work includes highly scalable geo-distributed services and an authentication library with nearly 500 million downloads. I also had fun integrating with hardware using Bluetooth (BLE). More here: https://www.sameer.blog/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 )

Facebook photo

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

Connecting to %s