1

In C++ it is possible to skip method parameter identifier if it's unused:

void foo( int, int ) {}

This is very useful in case of interface implementations where a huge number of method has empty body. Is it possible to do something similiar in C#? Straight test gives error:

public void OnAddInsUpdate( ref Array )  {} // Error, need identifier :(
grigoryvp
  • 40,413
  • 64
  • 174
  • 277
  • Interfaces imply you must implement all the methods by definition. You can make it an abstract class with empty implementation of the "unused" methods, leaving abstract the ones you require to be implemented. – Alex Bagnolini Nov 06 '09 at 13:25

6 Answers6

6

Afraid not - take a look at the language spec here:

rob
  • 1,154
  • 2
  • 12
  • 18
3

This is not supported by the C# language.

BennyM
  • 2,796
  • 16
  • 24
3

I don't believe this is possible. If you have unused parameters you should probably consider overloading or named parameters (new in C# 4.0).

Joe
  • 46,419
  • 33
  • 155
  • 245
1

It's not possible in C# you need to provide the parameter name, however, I don't see what's wrong with a check in the method to see if it has been set e.g.

public void OnAddInsUpdate(int x, int? y)
{
     // do something with x
     if (y != null)
        // do something with y
}

C# 4.0 introduces Optional Parameters.

James
  • 80,725
  • 18
  • 167
  • 237
  • int isn't an object so I don't think you can send it null. You would get a compile error. – Benjamin Autin Nov 06 '09 at 13:09
  • An integer will never be null, an int always has a value, 0 by default. – ynnckcmprnl Nov 06 '09 at 13:09
  • 1
    If you do use nullable types, like you now do in your code, it makes more sense to use the HasValue property instead of doing the != null check yourself. – ynnckcmprnl Nov 06 '09 at 13:17
  • Just out of interest, why use HasValue than the null check on nullable types? – badbod99 Nov 06 '09 at 13:24
  • I don't see any difference other than readability purposes. I would imagine the .HasValue property checks internally for value != null anyway. – James Nov 06 '09 at 13:28
  • Indeed, in the end it's the same, according to this question null checks are replaced by the compiler with a HasValue call. http://stackoverflow.com/questions/676078/which-is-preferred-nullable-hasvalue-or-nullable-null – ynnckcmprnl Nov 06 '09 at 13:30
1

Your example of an event with a number of arguments should actually be done using EventArgs.

This has a number of benefits. The primary one being that you can pass through a large number of potential values, but only populating the ones relevant.

The additional benefit is that you can at a later stage add more values without altering the signature of the event, and requiring all consumers to be updated.

So, for instance:

 public void OnAddInsUpdate(AddInsUpdateEventArgs e)  { } 

 public class AddInsUpdateEventArgs : EventArgs 
 { 
    public int x { get; }  // Readonly for event handler
    public int? y { get; set; }  // Read/Write (eg if you can accept values back) 
    public bool? Handled { get; set; } 
 } 
0

Maybe the use of params will give you what you want:

public class MyClass 
{

   public static void UseParams(params int[] list) 
   {
      for ( int i = 0 ; i < list.Length ; i++ )
         Console.WriteLine(list[i]);
      Console.WriteLine();
   }

   public static void UseParams2(params object[] list) 
   {
      for ( int i = 0 ; i < list.Length ; i++ )
         Console.WriteLine(list[i]);
      Console.WriteLine();
   }

   public static void Main() 
   {
      UseParams(1, 2, 3);
      UseParams2(1, 'a', "test"); 

      int[] myarray = new int[3] {10,11,12};
      UseParams(myarray);
   }
}
Philip Wallace
  • 7,905
  • 3
  • 28
  • 40