C
C#•16mo ago
r2d25551

When creating C# generic classes with static functions should the return type concreate class types?

For example:
public class MyClass<TKey, TValue> : IBase
{
TValue v;
public Dictionary<TKey, TValue> myData = new Dictionary<TKey, TValue>();

public Dictionary<TKey, TValue> GetData() { return myData; }

public static MyClass<string, MyData> CreateMyData()
{
MyClass<string, MyData> tmp = new MyClass<string, MyData>();
return tmp;
}
}
public class MyClass<TKey, TValue> : IBase
{
TValue v;
public Dictionary<TKey, TValue> myData = new Dictionary<TKey, TValue>();

public Dictionary<TKey, TValue> GetData() { return myData; }

public static MyClass<string, MyData> CreateMyData()
{
MyClass<string, MyData> tmp = new MyClass<string, MyData>();
return tmp;
}
}
And what about returning the same class from deserialization?
public static MyClass<string, MyData> Deserialize(string filename)
{
string jsonString = File.ReadAllText(filename);
MyCall<string, MyData> tmp = JsonSerializer.Deserialize<MyClass<string, MyData>>(jsonStrin);
return tmp;
}
public static MyClass<string, MyData> Deserialize(string filename)
{
string jsonString = File.ReadAllText(filename);
MyCall<string, MyData> tmp = JsonSerializer.Deserialize<MyClass<string, MyData>>(jsonStrin);
return tmp;
}
5 Replies
ero
ero•16mo ago
i'm unclear about the question? you likely wouldn't have a CreateMyData which returns one specific definition of your generic class so you could have
public class MyClass<TKey, TValue> : IBase
{
public static MyClass<TKey, TValue> CreateMyData()
{
MyClass<TKey, TValue> tmp = new MyClass<TKey, TValue>();
return tmp;
}
}
public class MyClass<TKey, TValue> : IBase
{
public static MyClass<TKey, TValue> CreateMyData()
{
MyClass<TKey, TValue> tmp = new MyClass<TKey, TValue>();
return tmp;
}
}
which would be entirely pointless because you can just do new MyClass<TKey, TValue>() anywhere in your code without that Create method you probably need to constrain TKey to notnull, and your GetData method is also pretty pointless
r2d25551
r2d25551OP•16mo ago
Makes sense. That actually was my initial problem. My original design was from a C++ perspective. The return type could be derived from the baseclass. But that caused problems for the compiler. Now I am rewriting my code, hopefully, that can use the new desing in another class. Dictionary<string, BaseClass> myData; Dictionay<string, DerivedClass> myData; . And using an interface for Dictionary<string, BaseClass> GetData() { . . } . That caused problems for me. So now I am trying to write with generics. . . am I headed in the right direction?
ero
ero•16mo ago
you'll always have this problem when you work with dictionaries all variations of dictionary have TKey and TValue as invariant
r2d25551
r2d25551OP•16mo ago
That would seem needed though. Almost like the compiler itself. C++ handles it differently. I am going to make my changes and see what happens. Bottom line, I want my worker class processing these classes to deal with a common and reliable interface. Thank you for the help. I will report back later of success or more problems 😄
Accord
Accord•16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server