Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you think generic properties would be useful in .NET?

I'm not talking about generic classes that declare properties or fields with the type of a generic parameter. I'm talking about generic properties which could be applied to both generic and non-generic classes.

I'm not talking about this:

public class Base<T>
{
    public T BaseProperty { get; set; }
}

I'm talking about this:

public class Base
{
    public T BaseProperty<T>
    {
       get
       {
          // Insert magic
       }
       set
       {
          // Insert magic
       }
    }
}

Or this:

public class Base<U>
{
    public T BaseProperty<T>
    {
       get
       {
          // Insert magic
       }
       set
       {
          // Insert magic
       }
    }

    public U OtherBaseProperty { get; set; }
}

The usage would go something like this:

var b = new Base();
b.BaseProperty<int> = 42;
int i = b.BaseProperty<int>;
b.BaseProperty<string> = "Hi";
string s = b.BaseProperty<string>;

Or for the second example:

var b = new Base<string>();
b.BaseProperty<int> = 42;
int i = b.BaseProperty<int>;
b.OtherBaseProperty = "Hi";
string s = b.OtherBaseProperty;

The // Insert Magic refers to handling each call to the generic property getter or setter that has a different type for the type parameter.

For example this:

b.BaseProperty<int> = 42;

Needs to be handled differently to:

b.BaseProperty<string> = "Hi";

I would envisage that for each type T if the getter is called before the setter is called then default(T) is returned. When the setter is called the value is stored per type T so that when the getter is subsequently called the previous value that was set for that type is returned.

Note that under the covers properties are just methods.

Do you think this would be useful?

like image 963
Jonathan Parker Avatar asked Mar 16 '09 23:03

Jonathan Parker


People also ask

What is the benefit of having a generic collection?

Generic collection types generally perform better for storing and manipulating value types because there is no need to box the value types. Generic delegates enable type-safe callbacks without the need to create multiple delegate classes.

Why generics are used in C#?

Use generic types to maximize code reuse, type safety, and performance. The most common use of generics is to create collection classes. The . NET class library contains several generic collection classes in the System.

When would you use generics in your code?

In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.

Which of the following is a generic type in C#?

In C#, generic means not specific to a particular data type. C# allows you to define generic classes, interfaces, abstract classes, fields, methods, static methods, properties, events, delegates, and operators using the type parameter and without the specific data type.


2 Answers

I've had a couple of times where I would have liked the ability to do this, yes.

However, the syntax involved would be pretty ugly, and it's sufficiently rarely useful that I think I prefer to just suck it up and go with generic methods.

like image 67
Jon Skeet Avatar answered Oct 05 '22 22:10

Jon Skeet


No .

like image 32
tsilb Avatar answered Oct 06 '22 00:10

tsilb