Vishful thinking…

Using ‘params’ as a part of delegate definition !!!***???

Posted in Uncategorized by viswaug on September 19, 2007

Today I just got curious about how the .NET compiler will handle a delegate definition with a ‘params’ parameter that will let you specify a variable number of parameters on a method. I know it doesn’t make sense but that i was curious as to how the .NET compiler will react to it. After a quick test, the results were somewhat expected. Here are my observations

  • To my dislike, the .NET compiler did not complain about the following definition

delegate void TestDelegate(int value, params object[] others);

  • I was actually able to create an instance of the delegate for the method with the following definition

No Error here

TestDelegate ni = new TestDelegate(IntCompareValidate);

Method definition

        private void IntCompareValidate(int value, object other1)

        {

            //Do something

        }

  •  But the compiler did not let me create an instance of the delegate for the method with the following definition

Error here. ” No overload for ‘IntBetweenValidate’ matches delegate ‘MockProject1.TestDelegate’ “

TestDelegate ni = new TestDelegate(IntBetweenValidate);

Method definition

        private void IntBetweenValidate(int value, object other1, object other2)

        {

            //Do something

        }

Just thinking a little bit more about it, I was thinking maybe the following would have been better. But then again I also do think that the .NET compiler should throw an error on the delegate definition instead of reacting the way it is.

No error here.

TestDelegate n1 = new TestDelegate(IntBetweenValidate);

TestDelegate n2 = new TestDelegate(IntCompareValidate);

 

Error here.

TestDelegate n1 = new TestDelegate(IntBetweenValidate);

n1 += new TestDelegate(IntCompareValidate);

 

What I am trying to say is that there should not be an error as long as all the functions added to the delegate have a consistent signature and there should be an error when they are not. But it is definitely going to be hard for the compiler to implement the above. But then again if you can do the above, the purpose of the delegates are kind of defeated. But it is just a thought…

What do you guys think?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: