Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generics, Comparing 2 strings fail unless explicitly specified

Tags:

c#

generics

I thought i've seen it all but this... :)

I was working on a generic graph of type string,

Graph<string> graph = new Graph<string>();

Graph is declared with a class constraint like this:

public class Graph<T> where T : class

Next i fill up the graph with some dynamicly generated strings:

for (char t = 'A'; t < 'J'; t++)
{
    GraphPrim.Add(t.ToString());
}

So far so good, (Node is a internal class containing the original value and a list of references to other nodes (because its a graph))

Now, when i try to create relations between the different nodes, i have to look up the right node by checking its value and thats where the weirdness starts.

The following code, is a direct copy of the result found in the immidiate window after doing some tests:

Nodes.First().Value
"A"
Nodes.First().Value == "A"
false
Nodes.First().Value.ToString() == "A"
true

Am i totally missing something or shouldn't Nodes.First().Value == "A" use a string comparison method. (The JIT compiler has knowledge about the type beeing used on runtime, and with that, its supported methods, right?). It seems to me like when not explicitly specifying a string, it will do a reference check rather then a string test.

It would be great if someone could explain this to me,

Thanks in advance!

like image 368
Polity Avatar asked Jan 20 '10 20:01

Polity


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 ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

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.

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.


1 Answers

If the types aren't fully known up front (i.e. Value is only known as T, and is not strictly known to be a string), use things like:

object.Equals(Nodes.First().Value,"A")

Of course, you could cast, but in this case you'd need a double-cast ((string)(object)) which is ugly.

If you know the two objects are the same type (i.e. two T values), then you can use:

EqualityComparer<T>.Default.Equals(x,y)

The advantage of the above is that it avoids boxing of structs and supports lifted Nullable<T> operators, and IEquatable<T> in addition to Equals.

like image 79
Marc Gravell Avatar answered Sep 20 '22 15:09

Marc Gravell