, where T: SomeType" if you know the type I am trying to understand the general limitations. Unfortunately, I have no reason t...">

Why use "List <T>, where T: SomeType" if you know the type

I am trying to understand the general limitations. Unfortunately, I have no reason to use them at the moment, but I try my best to come up with situations so that I can play with them and understand how they will help.

I have a problem. For me, these 2 are the same

public List<T> Get<T>() where T : DemoClassTwo
public List<DemoClassTwo> Get()

Both return Listand must have a type DemoClassTwo, and therefore I do not understand why I would use a method with this restriction.

Some actual codes to demonstrate this

static void Main(string[] args)
{
    var dco = new DemoClassOne();
    dco.Get().ForEach((a) => { Console.WriteLine(a.Id); });
    dco.Get<DemoClassTwo>().ForEach((a) => { Console.WriteLine(a.Id); });
    Console.ReadKey();
}

And supporting classes

public class DemoClassOne
{
    public List<T> Get<T>() where T : DemoClassTwo, new()
    {
        var result = new List<T>();
        var t = new T();
        t.Id = 1;
        result.Add(t);
        return result;
    }

    public List<DemoClassTwo> Get()
    {
        var result = new List<DemoClassTwo>();
        var d = new DemoClassTwo();
        d.Id = 1;
        result.Add(d);
        return result;    //Code here is pretty much identical to the other Get method
    }
}

public class DemoClassTwo
{
    public int Id { get; set; }
    public DemoClassTwo()
    {}
}

I just found a situation where 2 are the same or I will miss something, how it works.

#, Where T: ConcreteClass?, , , , ,

+4
4

, .

.

, , , , , . , .

, , List<DemoClassTwo>, , .

, , , .

, , , enitiy:

public abstract class BaseEntity
{
    public DateTime? DeletedOn { get; set; }
}

public class NotDeletedFilter<TEntity> 
    where TEntity : BaseEntity
{
    public IQueryable<TEntity> Filter(IQueryable<TEntity> entities)
    {
        return entities.Where(e => e.DeletedOn == null);
    }
}

, BaseEntity.

BaseEntity IBaseEntity, . , , . .

+5

. , - , ...

:

public List<T> Get<T>() where T : DemoClassTwo, new()

, , T ( ) DemoClassTwo .

public List<DemoClassTwo> Get()

, a List<DemoClassTwo>.

, DemoClassThree

public class DemoClassThree : DemoClassTwo
{
    public DemoClassThree() {
       Console.Write("New instance created !");
    }
}

Get, , List<DemoClassThree>:

var demoClassOneInstance = new DemoClassOne()
List<DemoClassThree> result =  demoClassOneInstance.Get<DemoClassThree>()

:

!

:

public class DemoClassFour
{
    public DemoClassFour() {
       Console.Write("New instance of DemoClassFour created !");
    }
}

List<DemoClassFour> result =  demoClassOneInstance.Get<DemoClassFour>()

... , DemoClassFour DemoClassTwo.

Icepickle, ... :)

+1

, Base Classes Interfaces. . DemoClassTwo , , , DemoClassOne

DemoClassBase, IDemoClass

, IDemoClass.

, , , .

DemoClassBase , , DemoClassBase, , . DemoBaseClass , . , , , , , .

0
source

I think the best explanation is Lasse Vågsæther Karlsen , said

public List<T> Get<T>() where T : DemoClassTwo

DemoClassTwoeach of its subclasses can return , but

public List<DemoClassTwo> Get() can only return DemoClassTwo

0
source

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


All Articles