I'm quite new to generics and I have a problem where two methods essentially do the same. The only difference is the parameter.
I have a json list, with a lot of JSON objects, and I have two methods getting the different objects in two different ways. The object is a language object consisting of an ID and code.
I have tried digging down in generics, since I think this is how I can solve this problem to make the code even nicer. However, I have a really hard time understanding it.
The methods look like this.
public LanguageDto GetLanguageById(int id)
{
var o = JObject.Parse(json);
var a = (JArray) o["list"];
var _list = a.ToObject<IList<LanguageDto>>();
return _list.FirstOrDefault(x => x.Id == id);
}
public LanguageDto GetLanguageByCode(string code)
{
var o = JObject.Parse(json);
var a = (JArray) o["list"];
var _list = a.ToObject<IList<LanguageDto>>();
return _list.FirstOrDefault(x => x.LanguageCode == code);
}
What I would like is a combined method that looks like this
public LanguageDto GetLanguage(T)
{
var o = JObject.Parse(json);
var a = (JArray) o["list"];
var _list = a.ToObject<IList<LanguageDto>>();
return _list.FirstOrDefault(x => x.T == T);
}
I hope you get my pseudo code.
If someone has some great learning resources on Generics or something that touches this problem, I would appreciate if you would share.