C# "var" argument in method without using generics (templates) -
i have set of simple function identical except passed data types. data types basic types.
i tried use "var" expected compiler not know how handle functions internal types.
do need use generics (templates) here?
or there other built in featured c# not aware of?
thanks in advance help!
sean
protected bool checkpropertychanged(ref bool attribute, bool newvalue) { bool propertychanged = false; if (attribute != newvalue) { propertychanged = true; attribute = newvalue; } return propertychanged; } protected bool checkpropertychanged(ref string attribute, string newvalue) { bool propertychanged = false; if (attribute != newvalue) { propertychanged = true; attribute = newvalue; } return propertychanged; }
i used solution below found attribute not being modified when using generics, regardless of how called method. reverted original code of having separate methods , revisit... didn't take time investigate... possible i'm doing stupid here.
static bool checkpropertychanged<t>(ref t attribute, t newvalue) { if (!attribute.equals(newvalue)) { attribute = newvalue; return true; } return false; }
yes generics can 1 solution, can try somehting like
static bool checkpropertychanged<t>(ref t attribute, t newvalue) { if (!attribute.equals(newvalue)) { attribute = newvalue; return true; } return false; }
and call:
checkpropertychanged<bool>(ref a, b)
or
checkpropertychanged<string>(ref a, b)
or whatever type want. make sure .equals returns expect depending of types want compare.
i think generics clean solution. there reason why want without generics?
Comments
Post a Comment