Mam problem ze zrozumieniem, jak działają ograniczenia ogólne. Chyba brakuje tu czegoś ważnego. Załączam moje pytania w komentarzach i będę wdzięczny za wyjaśnienia.Ogólne ograniczenie na T, aby typ odniesienia i typ wartości jednocześnie?
//1st example:
class C <T, U>
where T : class
where U : struct, T
{
}
//Above code compiles well,
//On first sight it looks like U might be reference type and value type
//at the same time. The only reason I can think of, is that T may be an
//interface which struct can implement, Am I correct?
//2nd example
class CC<T, U>
where T : class, new()
where U : struct, T
{
}
//I added also a reguirement for parameterless constructor
//and, much to my surprise, it still compiles what is
//a bit inexplicable for me.
//What 'U' would meet the requirement to be
//value type, reference type and have a contructor at the same time?
Tak? i ? Podałeś go dla T, który byłby zgodny z konstruktorem obiektów. W porządku. –
Tak, masz rację, przeczytałem źle. @Mark Byers powiedziałeś, że wszystkie structs pochodzą z 'object'. SO Jeśli miałbym 'class CS gdzie T: class {}' Code like 'CS obj = new CS ();' nie działałoby, podczas gdy 'DateTime' można rzutować na obiekt. –
nan
@Andrzej Nosal: Tak, ponieważ ograniczenie "T: klasa" nie oznacza, że * T można rzutować na obiekt *. Oznacza to * T musi być typem referencyjnym *. DateTime nie jest typem referencyjnym. –