c# - What "where" clause will make this Generic Method work? -
consider admittedly contrived generic definition:
private void foo<t,basetype>(propertyinfo prop, basetype o1, basetype o2) { t value1 = (t) prop.getvalue(o1, null); t value2 = (t) prop.getvalue(o2, null); if (value1 != value2) console.writeline("not equal"); }
prop guaranteed propertyinfo basetype.
i getting compile error @ if() statement:
operator '!=' cannot applied operands of type 't' , 't'
while in "general case" understand error message valid, in case, want routine of standard types: system.int64, system.string, etc of support == , != operator.
i assume can fixed "where" clause, icomparable , iequalable don't help.
do know correct "where" clause is?
frank
since system.int64, system.string, etc .. list implement icomparable
, use
where t : icomparable
and use compareto()
instead of !=
for eg. code compile
private void foo<t>(object o) t : icomparable { t v1 = default(t); t v2 = default(t); if(v1.compareto(v2) != 0) { console.writeline("not equal"); } } private void bar() { foo<string>(new object()); }
Comments
Post a Comment