Void/function on a class which is an array

  • Thread starter Thread starter Oyvind Eriksen
  • Start date Start date
O

Oyvind Eriksen

Hello.
Look at the code below.
I'm trying to create a function/void for when a class is declared as an
array.
Is it possible? If so, how?

Regards,
Oyvind Eriksen


public class Main
{
SomeClass[] array = SomeClass[10];
array.MyVoid(); <--- how do I create this void?
}
public class SomeClass
{

}
 
Look at the code below.
I'm trying to create a function/void for when a class is declared as an
array.
Is it possible? If so, how?

No, you can't do that. You could create a static method in SomeClass
which takes a SomeClass[] as a parameter though.
 
The next release of C# (3.0) will include a new feature called
"extension methods" that will let you add new method to existing types.
However, I'm not sure if this will include array types, if not then
you could probably add your method to the Array base class. Either
way, this probably isn't an ideal solution, and you might be better off
passing your array as a parameter to another method.
 
Jon Skeet said:
Look at the code below.
I'm trying to create a function/void for when a class is declared as an
array.
Is it possible? If so, how?

No, you can't do that. You could create a static method in SomeClass
which takes a SomeClass[] as a parameter though.

I see. It would be practical though... :-)
But a static method with SomeClass[] would solve the problem.
Or create a collection?

Regards,
Oyvind Eriksen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top