質問

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

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

役に立ちましたか?

解決

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top