< > tags in C#

7 replies [Last post]
Posts: 14

what does the < > mean in c#?

Example
readonly Queue<IIPSocket> _disconnectedSockets = new Queue<IIPSocket>();

Ive seen Queue() but I dont know what Queue<> is.

Posts: 1691

Generics

http://msdn.microsoft.com/en-us/library/ms379564.aspx

In this case, and for stuff like List<>, IEnumerable<>, Stack<>, etc, the generic parameter states what kind of object is in the collection. If you just use List, it will contain the type "object". It just gives you stronger typing, and in some cases, improves performance. Also allows you to avoid having to down-cast so much.

Posts: 14

Wow quick response!

Thanks Spodi Smile

Posts: 1030

Yeah, for example in java there's also int[] for an array but that's the old way of writing it. Apparently the guys who maintain the java code made it that way so programmers from other languages wouldn't be confused, ironically the 'newer' collections do have < >

Posts: 458

bad english on my side.

so it brings pros against the () tags...
is it usable in the same manner? (i dont mean EXACT syntax, but usage of the "arrays" you create in this style)

and what is "stronger typing"?
and what is down-cast?

all my books are german, learning english definitions is a bit complex sometimes ^^

Posts: 1691

Ok, when you create a class, struct, delegate, or method, you can specify a generic parameter. On a class-level, it is like you see with List<>, where it comes immediately after the class name. To simply it, think of it as if you were to just remove the <> from the declaration. The generic parameter has a name, just like any other parameter in programming, so you can reference it. People usually use "T", or something that starts with "T", just since that is usually what people do for C# (T standing for Type Parameter). Instead of an actual value, this parameter is a type. So imagine this:

class Stack<T> {
   public void Push(T item) { ... }
   public T Pop() { ... }
}

If you replace the "T" with the name of the type you want, and remove the angular brackets:

class StackInt {
   public void Push(int item) { ... }
   public int Pop() { ... }
}

Its just like if you were to create a class like that.

So basically, it allows the user of the class to specify some types to modify the behavior of the class.

"stronger typing" means that you are using the type you want. A List with no generics would be like having an object[]. If you have a bunch of integers, would you put it into an object[], or int[]? Obviously int[], since you would put it into whatever type it actually is.

"down-casting" is casting to a more specific type. For instance, the hierarchy for a User class in NetGore is (from most to least specific):

User : Character : CharacterEntity : DynamicEntity : Entity

"down-casting" is going from a less-specific type (such as Entity) to a more-specific type (anything to the left of Entity). So casting a CharacterEntity to User is down-casting. Casting a Character to Entity is up-casting.

Down-casting is generally good to avoid since you one type may take on many forms. For instance, there are tons of different types of Entities - walls, teleports, items, users, NPCs, etc. If you try to cast from Entity to User, it could fail. Up-casting, however, is always fine. A User is always going to be an Entity, so the operation is always going to be safe.

Posts: 458

hmm that sounded much like overloading or what the english word would be.

in that case you had to work with multiple constructors. one per possible type.

ill look into the link you gave, thanks for that detailed answer.

im tired now, will read again tommorrow when i have time and had sleep.

Posts: 531

It's basically saying the Type <T> can be used in a class where T is a particular type, this means that if you had a Class, such as List for example. Instead of creating a ListInt class and a ListDouble class all you need to do is where the Type that changes appears in the main code of the Class T will appear. Edit: think of T as a variable in the code where a particular type will appear where T is.

Like in Spodi's example:

class Stack<T> {
   public void Push(T item) { ... }
   public T Pop() { ... }
}
 
class StackInt {
   public void Push(int item) { ... }
   public int Pop() { ... }
}

Gwen wrote:
what does the < > mean in c#?

Example
readonly Queue _disconnectedSockets = new Queue();

Ive seen Queue() but I dont know what Queue<> is.

the Queue() is a constructor for the class Queue and will always be seen like this = new Queue<TYPE>();, similar to a sub routine or function, and the Queue is just a class that has been associated with the Type IIPSocket. The Queue() function will be contained INSIDE the class Queue and as you can see in the above example it is called as the constructor here: = new Queue<IIPSocket>() notice the Type in the <> brackets and the () to show that it is a sub routine or function, in this case it is the Constructor.

I hope i made an ounce of sense Tongue

There are 10 types of people in this world - Those who understand Binary and those who don't.