Stratton
Stratton

Reputation: 3603

using the 'class' keyword as/in a method argument in c#

I'm not sure where I saw this, and I'm certainly not getting the syntax past the compiler. Is it possible to use the 'class' C# keyword as part of a method parameter signature?

foo(string x, class y) { }

Anyone else see something like this? thanks, -gene

Upvotes: 3

Views: 402

Answers (4)

Gregoire
Gregoire

Reputation: 24872

It is possible to use the word class in in a generic method definition:

foo<T>(T object) where T:class

Upvotes: 2

MattH
MattH

Reputation: 4227

Use object if you want to be able to pass y as anything:

foo(string x, class y) { }

use generics if you want to state y has to be an object of a class - not a struct or an internace for example.

foo<MyType>(string x, MyType y) where MyType : class { }

Upvotes: 0

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158379

I don't think you can use it as in your example, but if you would want to use the word "class" as a parameter name, that is doable by prefixing it with @:

foo(string @class) { }

Upvotes: 2

maxpower47
maxpower47

Reputation: 1656

Should you be using object maybe? It looks like you are trying to specify a parameter that can have any type, in which case you should use object, since everything derives from it.

Upvotes: 8

Related Questions