Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# compare two generic values [duplicate]

Tags:

c#

generics

Possible Duplicate:
Can’t operator == be applied to generic types in C#?

I've coded something like this:

public bool IsDataChanged() {                T value1 = GetValue2;     T value2 = GetValue1();      return (valueInDB != valueFromView); } 

Right now the function doesn't compile with the error "Operator '!=' cannot be applied to operands of type 'T' and 'T'". What do I have to do to make this function work ?

like image 854
bernhardrusch Avatar asked Jan 28 '09 16:01

bernhardrusch


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

You cannot use operators on generic types (except for foo == null which is special cased) unless you add where T : class to indicate it is a reference type (then foo == bar is legal)

Use EqualityComparer<T>.Default to do it for you. This will not work on types which only supply an operator overload for == without also either:

  • implement IEquatable<T>
  • overrides object.Equals()

In general implementing the == operator and not also doing at least one of these would be a very bad idea anyway so this is not likely to be an issue.

public bool IsDataChanged<T>() {                T value1 = GetValue2;     T value2 = GetValue1();      return !EqualityComparer<T>.Default.Equals(value1 , value2); } 

If you do not restrict to IEquatable<T> then the EqualityComparer default fallback may cause boxing when used with value types if they do not implement IEquatable<T> (if you control the types which are being used this may not matter). I am assuming you were using =! for performance though so restricting to the Generic type will avoid accidental boxing via the Object.Equals(object) route.

like image 75
ShuggyCoUk Avatar answered Sep 28 '22 05:09

ShuggyCoUk


That should work for you.

public bool test<T>(T test, T test2) where T : class {     return (test != test2); } 

This is simply pasted from the examples that were commented on your question.

like image 34
CalvinR Avatar answered Sep 28 '22 04:09

CalvinR