What is the difference between these method signatures?
public void T MyMethod<T>(T parameter)
and
public void T MyMethod<T>(T parameter) where T : class
They seem to have the same result ... so what does where T : class do?
where T : class
In the second method, T can only be a class and cannot be a structural type.
See Type Parameter Limitations (C # Programming Guide) :
where T: classType must be a reference type [class]; this also applies to any class, interface, delegation, or array type.
where T: class
Type must be a reference type [class]; this also applies to any class, interface, delegation, or array type.
in the first case, you can call it using a non ref type, for example
MyMethod<int>(10);
which will not work with the second version, since it only accepts link types!
no difference, but T is limited to a reference type. they differ only in compiletime, since the compiler checks that T is a ref type or not.
void
T
MyMethod(1)
Source: https://habr.com/ru/post/1400883/More articles:Reprogramming interrupt in c / asm x86 - assemblyMake wallpaper visible in Google Chrome extension - google-chromeWhat is the difference between calling setSoLinger with a value of 0 and not including soLinger? - javaWhat correct exception throws the intended place out of reach? - .netJava network server and TIME_WAIT - javaGet the System32 directory in the kernel - cUnable to add object to NSMutableArray - objective-cVisual Studio 2010 html indentation - htmltcp connection in TIME_WAIT will not allow reconnecting, java - javahttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1400888/cannot-add-items-to-an-nsmutablearray-ivar&usg=ALkJrhg2S-HpCSBlVOxEXhN0zZt-9S0XEwAll Articles