Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

.net - What is the "< >" syntax within C#

I have been learning about the basics of C# but haven't come across a good explanation of what this is:

var l = new List<string>();

I don't know what the <string> is doing or if it's the List that is doing the magic. I have also seen objects been thrown within the < > tags.

Can someone explain this to me with examples, please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That is the generic syntax for C#.

The basic concept is that it allows you to use a Type placeholder and substitute the actual real type in at compile time.

For example, the old way:

ArrayList foos = new Arraylist();
foos.Add("Test");

worked by making ArrayList store a list of System.Objects (The base type for all things .NET).

So, when adding or retrieving an object from the list, The CLR would have to cast it to object, basically what really happens is this:

foos.Add("Test" as System.Object);
string s = foos[1] as String.

This causes a performance penalty from the casting, and its also unsafe because I can do this:

ArrayList listOfStrings = new ArrayList();
listOfStrings.Add(1);
listOfStrings.Add("Test");

This will compile just fine, even though I put an integer in listOfStrings.

Generics changed all of this, now using Generics I can declare what Type my collection expects:

List<int> listOfIntegers = new List<int>();
List<String> listOfStrings = new List<String>();

listOfIntegers.add(1);

// Compile time error.
listOfIntegers.add("test");

This provides compile-time type safety, as well as avoids expensive casting operations.

The way you leverage this is pretty simple, though there are some advanced edge cases. The basic concept is to make your class type agnostic by using a type placeholder, for example, if I wanted to create a generic "Add Two Things" class.

public class Adder<T>
{
   public T AddTwoThings(T t1, T t2)
   {
       return t1 + t2;
   }
}

Adder<String> stringAdder = new Adder<String>();
Console.Writeline(stringAdder.AddTwoThings("Test,"123"));

Adder<int> intAdder = new Adder<int>();
Console.Writeline(intAdder.AddTwoThings(2,2));

For a much more detailed explanation of generics, I can't recommend enough the book CLR via C#.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...