That indicates the presence of restrictions for type parameters in .NET (base class and interface restrictions)

I am working on a small class library at work, and this naturally involves using generics for this task. But there is such a thing that I really don’t understand with generics: Why do I need to use type parameters, and then restrict the type parameter to a specific base class or interface.

Here is an example of what I mean:

public class MyGenericClass<T> where T : SomeBaseClass { private T data; } 

And here is a generic-free implementation

 public class MyClass { private SomeBaseClass data; } 

Are these two definitions the same (if so, then I don’t see the benefits of using generics here)?

If not, then why should we use generics here?

+6
source share
5 answers

Like almost all uses of generics, the benefits come to the consumer. A type restriction gives you the same advantages that you get by strictly typing your parameter (or you can do other things, for example, make sure that there is an open constructor without parameters or provide it with either a value or a reference type), while preserving all the subtleties generics for the consumer of your class or function.

Using generics also, for example, allows you to get the actual type that was specified if it has any particular value.

This example is a little contrived, but look at this:

 public class BaseClass { public void FunctionYouNeed(); } public class Derived : BaseClass { public void OtherFunction(); } public class MyGenericClass<T> where T: BaseClass { public MyGenericClass(T wrappedValue) { WrappedValue = wrappedValue; } public T WrappedValue { get; set; } public void Foo() { WrappedValue.FunctionYouNeed(); } } ... var MyGenericClass bar = new MyGenericClass<Derived>(new Derived()); bar.Foo(); bar.WrappedValue.OtherFunction(); 
+7
source

The difference is that the former defines the new class as a specific type; the latter simply defines a simple class with a field of this type.

+1
source

All about security type. Using generics, you can return a specific type (T) instead of some base type that defines the API that you need in your common class. Thus, the caller of your method should not cast the result to a specific type (which is an error prone operation).

+1
source

The main difference is in use. In the first case, the use may have:

 MyGenericClass<SomeDerivedClass> Variable Variable.data.SomeDerivedProperty = X 

And so that when using this class you can still access something from SomeDerivedClass without returning to it.

The second example will not allow this.

 MyClass.data = SomeDerivedClassInstance MyClass.data.SomeDerivedProperty = X //Compile Error ((SomeDerivedClass)MyClass.data).SomeDerivedProperty = X //Ewwwww 

You will need to back up to SomeDerivedClass (which is unsafe) in order to use something specific for the derived class.

+1
source

I don’t think there is a huge difference, except that the general version limits your class , while the second is just a restriction on the class member . If you add more members and methods to your first class, you will have the same restriction.

+1
source

Source: https://habr.com/ru/post/914539/


All Articles