Mladen Prajdić Blog

Blog about stuff and things and stuff. Mostly about SQL server and .Net

C# 2.0: Arity - ever heard of it?

 

Arity is the number of arguments that a method takes.

For example

void MyMethod(int i1, int i2)

 

has an arity of 2, since it takes 2 arguments.

In C# the arity is marked with `

 

"So why is this important?", you might ask.

Well it isn't. Until you deal with reflection. :)

A while ago I had to create some code using CodeDOM and i was getting a few errors that the ` in code isn't correct

when i tried to compile the code.

 

Here's an example of how arity is shown using reflection:

List<string> list = new List<string>();
Console.WriteLine(list.GetType().ToString());

// output: System.Collections.Generic.List`1[System.String]

Dictionary<int, string> dict = new Dictionary<int, string>(); Console.WriteLine(dict.GetType().ToString());

// output: System.Collections.Generic.Dictionary`2[System.Int32,System.String]

 

You can see that the ` is used to tell us how many arguments a generic type uses.

kick it on DotNetKicks.com

Legacy Comments


Ayende Rahien
2007-06-14
re: C# 2.0: Arity - ever heard of it?
I think you are confusing something, you are talking about GENERIC parameters, not normal params

Eric
2007-06-14
re: C# 2.0: Arity - ever heard of it?
Ayende is right... it<s seem to be only for GENERICS... but that<s cool to know that, i tried to use reflection often as i can in my projects.

Mladen
2007-06-14
re: C# 2.0: Arity - ever heard of it?
emm... confusing? no not really.

Arity in .net is displayed only for generic params
and it shows how many of them the type needs.

maybe it's displayed for methods in some way too, but
i haven't seen it so far.

the void MyMethod(int i1, int i2) was only an example of what arity means.