Domanda

void LoadParameters<T, TValue>(TValue strategy)
        where T : Parameters

Saw a code like this. Will the program still work correctly without the where statement?

È stato utile?

Soluzione

If it compiles without the where statement, it will work correctly. However, chances are the coder did not simply put it there without reason. If you remove it, it will likely not compile and hence not work.

Altri suggerimenti

That where clause is called a "generic type constraint". Usually T can be any type and you must write your generic code with that in mind. By constraining T to be, inherit or implement a specific type, you gain the ability to refer to the members of that type in your generic code, because the compiler is assured that any object used will be that type. You can also use class, struct or new as generic type constraints, which enforce T being a reference type, a value type or having a parameterless constructor respectively.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top