开发者

In C#, What is <T> After a Method Declaration?

I'm a VB.Net guy. (because I have to be, because the person who signs my check says so. :P) I grew up in Java and I don't generally struggle to read or write in C# when I get the chance. I came across some syntax today that I have never seen, and that I can't seem to figure out.

In the following method declaration, what does < T > represent?

static void Foo < T >(params T[] x)

I have seen used in conjunction with declaring generic collections and things, but I can't for the life of me figure out what it does for 开发者_开发知识库this method.

In case it matters, I came across it when thinking about some C# brain teasers. The sixth teaser contains the entire code snippet.


what you are asking is the concept of the generics in c#. By using generics you can use this method for the types you want

suppose you have to create a function to add two numbers. In that case, your function is

//For integer :
public int sum(int a, int b)
{ 
  return a + b;
}



//For floating point numbers :
public float sum( float a, float b)
{
  return a + b;
}

Following this logic, if you want a function that will sum two double type numbers you would create one more function, and so on.

Note: code above will not work with C#, but it for explaining concept easily, it just sudo code it will work with C# if you have nullable type or reference type easily or one need to write logic to convert value to primary type.

Bu with the help of generics you can replace all of these functions and write the following,

public T sum<T>(T a, T b)
{
  return a + b;
}

This will work for all numeric types, as well as for strings.

check this out for more detail : http://www.codeproject.com/kb/books/EssentialCS20.aspx


This defines a generic method, which is one form of generics, which were added to C# in C# 2.0.

The method sig should be:

static void Foo<T>(params T[] x)
{ // ...

This lets you pass any number of arguments of any (specific) type into the method Foo, and it acts on that array of arguments. It's similar to how generic types work, except scoped just to the method. The <T> specifies the type of the argument being passed into the method, so you can call this like:

Foo<MyClass>(myClassInstance, myClassInstance2, mySubclassInstance);


The T is a type parameter and in this case can be anything (a constraint may be specified, but here there is no constraint). Without this feature you would have to declare the method for every type you plan to use:

static void Foo(params int[] x)    
static void Foo(params string[] x)    
static void Foo(params Customer[] x)    
etc...

More information on generics can be found on MSDN: Introduction to Generics (C#).


That is the Generic Type parameter of a Generic Method.

Once you specify a type for T when calling the method, .NET can ensure type safety based on that type parameter.

static void Foo<T>(params T[] x) { }

would be called like:

string[] names = new string[] {"Foo", "Bar", "Baz"};

Foo<string>(names);

but this would cause a compiler error:

int[] nums = new int[] {1, 2, 3};

Foo<string>(nums); // nums is not string[]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜