简体   繁体   中英

Generic Interface in C# migrated from JAVA

I want to convert this interface members to C#

<T> T GetValue(string areaKey, string key, Class<T> clazz);

<T> T GetValue(string areaKey, string key, T defaultValue, Class<T> clazz);

but I am not so good with C# as with JAVA. Is it possible to use it in the same way as in JAVA?

The generic type is just specified after the method name in C#

T GetValue<T> (string areaKey, string key, Class<T> clazz);

or, you can specify it on the interface itself:

public interface IMyInterface<T>
{
    T GetValue (string areaKey, string key, Class<T> clazz);
}

yup. You could try something like below:

public interface InterfaceName<T>
{
    T GetValue(string areaKey, string key, T clazz);
    T GetValue(string areaKey, string key, T defaultValue, T clazz);
}

Christos is correct, I would just add where T : class

public interface InterfaceName<T> where T : class
{
    T GetValue(string areaKey, string key, T clazz);
    T GetValue(string areaKey, string key, T defaultValue, T clazz);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM