What does Method<ClassName> mean?

I've seen this syntax a couple times now, and it's beginning to worry me,

For example:

iCalendar iCal = new iCalendar();
Event evt = iCal.Create<Event>();

Answers


It's a Generic Method, Create is declared with type parameters, and check this links for more information:


It's calling a generic method - so in your case, the method may be declared like this:

public T Create<T>()

You can specify the type argument in the angle brackets, just as you would for creating an instance of a generic type:

List<Event> list = new List<Event>();

Does that help?

One difference between generic methods and generic types is that the compiler can try to infer the type argument. For instance, if your Create method were instead:

public T Copy<T>(T original)

you could just call

Copy(someEvent);

and the compiler would infer that you meant:

Copy<Event>(someEvent);

It is a generic method that implements the Factory Method pattern.


This syntax is just applying generics to a method. It's typically used for scenarios where you want to control the return type of the method. You will find this kind of syntax a lot in code that uses a IoC framework.


It's the way you mention a generic method in C#.

When you define a generic method you code like this:

return-type MethodName<type-parameter-list>(parameter-list)

When you call a generic method, the compiler usually infers the type parameter from the arguments specified, like this example:

Array.ForEach(myArray, Console.WriteLine);

In this example, if "myArray" is a string array, it'll call Array.ForEach<string> and if it's an int array, it'll call Array.ForEach<int>.

Sometimes, it's impossible for the compiler to infer the type from the parameters (just like your example, where there are no parameters at all). In these cases, you have to specify them manually like that.


Need Your Help

Lightweight SQL editor for Eclipse

sql eclipse editor eclipse-plugin

Is there any simple SQL editor plugin for Eclipse?

Insert a char* at the beginning of another char* C++

c++ string pointers char

I was wondering if there´s any way at all of inserting a char* at the beginning of another char*.