R
Rene
I was experimenting with lambda expressions this afternoon and created the
following snippet of code:
--------------------------------
delegate void Del1(byte b1);
delegate void Del2(int i1);
class FooBar
{
static void DoIt(Del1 del1)
{
}
static void DoIt(Del2 del2)
{
}
static void Main(string[] args)
{
DoIt((x) => System.Console.WriteLine(x.GetType().ToString()));
}
}
--------------------------------
The code above will throw the following compile error: The call is ambiguous
between the following methods or properties: 'Program.DoIt(Del1)' and
'Program.DoIt(Del2)'.
This is all very obvious and very easy to fix but the thing that I want to
point out is that if the snippet of code above only had one "DoIt()" method
then things would compile just fine the way they are. But as soon as you add
the second "DoIt()" overloaded method you get the ambiguity.
So in this example, the lack of explicitly specifying the parameter type on
the lambda expression such as:
DoIt((byte x) => System.Console.WriteLine(x.GetType().ToString()));
DoIt((int x) => System.Console.WriteLine(x.GetType().ToString()));
can cause problems in the future if the class is enhanced (such as adding
the second "DoIt" overloaded method in this case).
Now, here is the problem, supposed that the FooBar class (containing only
*one *DoIt method) was inside a dll that it's widely referenced by many
applications in your company. Also supposed that there are hundreds of
procedures that call the DoIt() method.
Now think about all the code that would break if someone added the second
overloaded DoIt() method to the FooBar class! And all of these problems
caused just because people did not fully qualify the parameter on the lambda
expression.
I realize that my example may be far-out but I am sure you get the point.
So my question to you to ponder is... Is the compiler really doing us a
favor by saving us from typing just a couple of characters?
And by the way, you can run to similar issues due to the delegate
variance/covariance feature and the compiler eagerness to try to guess what
you want to do, see snippet below:
--------------------------------
delegate void MyDel(System.IO.Stream o);
class Program
{
static void Main(string[] args)
{
MyDel d = new MyDel(DoIt);
var ms = new System.IO.MemoryStream();
d(ms);
System.Console.Read();
}
static void DoIt(object o)
{
System.Console.WriteLine("object");
}
// If this method did not exist, MyDel delegate (on
// the Main method) will bind to the DoIt() method
// with the object parameter but when this method
// is added it will bind to this method! Sometimes
// this may not be what you want!
static void DoIt(System.IO.Stream s)
{
System.Console.WriteLine("Stream");
}
}
--------------------------------
following snippet of code:
--------------------------------
delegate void Del1(byte b1);
delegate void Del2(int i1);
class FooBar
{
static void DoIt(Del1 del1)
{
}
static void DoIt(Del2 del2)
{
}
static void Main(string[] args)
{
DoIt((x) => System.Console.WriteLine(x.GetType().ToString()));
}
}
--------------------------------
The code above will throw the following compile error: The call is ambiguous
between the following methods or properties: 'Program.DoIt(Del1)' and
'Program.DoIt(Del2)'.
This is all very obvious and very easy to fix but the thing that I want to
point out is that if the snippet of code above only had one "DoIt()" method
then things would compile just fine the way they are. But as soon as you add
the second "DoIt()" overloaded method you get the ambiguity.
So in this example, the lack of explicitly specifying the parameter type on
the lambda expression such as:
DoIt((byte x) => System.Console.WriteLine(x.GetType().ToString()));
DoIt((int x) => System.Console.WriteLine(x.GetType().ToString()));
can cause problems in the future if the class is enhanced (such as adding
the second "DoIt" overloaded method in this case).
Now, here is the problem, supposed that the FooBar class (containing only
*one *DoIt method) was inside a dll that it's widely referenced by many
applications in your company. Also supposed that there are hundreds of
procedures that call the DoIt() method.
Now think about all the code that would break if someone added the second
overloaded DoIt() method to the FooBar class! And all of these problems
caused just because people did not fully qualify the parameter on the lambda
expression.
I realize that my example may be far-out but I am sure you get the point.
So my question to you to ponder is... Is the compiler really doing us a
favor by saving us from typing just a couple of characters?
And by the way, you can run to similar issues due to the delegate
variance/covariance feature and the compiler eagerness to try to guess what
you want to do, see snippet below:
--------------------------------
delegate void MyDel(System.IO.Stream o);
class Program
{
static void Main(string[] args)
{
MyDel d = new MyDel(DoIt);
var ms = new System.IO.MemoryStream();
d(ms);
System.Console.Read();
}
static void DoIt(object o)
{
System.Console.WriteLine("object");
}
// If this method did not exist, MyDel delegate (on
// the Main method) will bind to the DoIt() method
// with the object parameter but when this method
// is added it will bind to this method! Sometimes
// this may not be what you want!
static void DoIt(System.IO.Stream s)
{
System.Console.WriteLine("Stream");
}
}
--------------------------------