Eric Gunnerson - What has the biggest change at Microsoft since you've been here?

First of all C# rocks!!!
My question is about optional parameters in C#, or there lack of. VB.NET & CLR has support for them, but not C#. I have been hoping that C# 2.0 (Whidbey) will include support for optional parameters, but it seems that it will not.
What is the reasoning for not including optional parameters (aka default parameters) support with C#?
eg...
public void MyMethod(int num, string text = “default text”)
{
...
}
cheers,
Mike M
WinInsider.com
I think I've seen the reasoning for this from other languages (Java I believe). THe reason I recall is that you can handle optional parameters with method overloading and this also alleviates some ambiguity problems as well.
eg.
public void MyMethod(int num) {
MyMethod(num, "default text");
}
public void MyMethod(int num, string text = “default text”)
{
...
}
Uwe Keim wrote:Once I read about an exprimental feature in Java that enables code that CATCHES an exception to be able to tell the original code that has THROWN the exception to continue to execute.
Such a feature would be handy in C# as well, I guess
You are not wrong. In Java you can define which exceptions a method will throw by using the throws keyword. Consumers will know ahead of time which exceptions to catch. Anders explains the shortcomings of this pattern in the article I mentioned above.
You seem to be interested in exceptions. This is good
What do you think of the exception model currently in place in the Framework?
Charles
Fancy code refactoring with extentension interface implemenations
I have been playing around with the wonderfull new extension methods, templates etc.
You can do some great things with the new IEumerator<T> list constructs.
One of the things i really like about these is that you can apply them consistently accross a number of different types.
It seemed logical to do the same type of thing with trees however here-in lies a problem there is no consistantly used interfce such as IEnumerator upon which to base such extensions.
You can of course create the same code multiple times each one for a different type, but this seems really messy and inefficient.
What would be usefull would to provide allow extension interface implementations - a bit like a multiple inheritance extension.
We could have something like:
public interface ITreeBase<T> // just a normal everyday interface
{
bool IsRoot();
IEmumerable<T> GetChildren();
}
// Now say we want extension methods to implement this inteface for a TreeNode we might have something like
public static class TreeNodeExtn:ITreeBase<TreeNode>
{
public bool IsRoot(this TreeNode This) { return false; }
public IEmumerable<TreeNode> GetChildren(this TreeNode This)
{
return This.Nodes;
}
}
// The above is ok - but we need to be specific about what class we are adding the inteface extension to.
// Also it would be nice to simplify the syntax a bit. So we we might re-write this something like:
public class TreeNodeExtn extends TreeNode :ITreeBase<TreeNode> // Note this is still just an Extension no real inheritence here
{
public bool IsRoot { get { return false;} }
public IEmumerable<TreeNode> GetChildren()
{
return this.Nodes;
}
}
OK so now we define this for some other types also as follows:
public static class TreeViewExtn extends TreeView: ITreeBase<TreeNode>
{
public bool IsRoot { get { return true; }}
public IEmumerable<TreeNode> GetChildren()
{
return This.Nodes;
}
}
public static class XPathNavigatorExtn extends XPathNavigator: ITreeBase<XPathNavigator> // note this is a static class with extension methods
{
public bool IsRoot { get {return false; }}
public IEmumerable<XPathNavigator> GetChildren()
{
while(var v in this.Children()) yield return v.current;
}
}
Now we can of course use these extension methods exactly as in before for items of one of the TreeNode,TreeView or XPathNavigator
So next we would like to write some generic tree handling extension methods using our new extensions as follows
public static class TreeBaseExtnMethods
{
public IEmumerable<T> Descendants<T>(this ITreeBase<T> This)
{
foreach(var v in This.Children())
{
yield return v;
foreach(var d in v.SelfAndDescendants())yield return d;
}
}
public IEmumerable<T> FindDescendant<T>(this ITreeBase<T> This,Func<T,bool> fn)
{
foreach(var v in This.Children())
{
if(fn(v)){ yield return v; yield break; }
foreach(var d in v.SelfAndDescendants(fn))yield return d;
}
}
.....
}
// OK looks nice but how will this work in practice.
// The complier will compile the TreebaseExtn methods as before.
// So for it to work the above functions actually need to be passed a class that implements ITreeBase<T>
// To achive this the compiler simply creates an instance class of each of the extension classes (TreeNode,TreeView or XPathNavigator).
// These class are simply container classes each one has exactly just one field "this" which refers to an instance of the object we are extending.
// So for the first example it in effect complies as:
public class TreeNodeExtn:ITreeBase<TreeNode>
{
prviate TreeNode This;
public bool IsRoot { get { return false;} }
public IEmumerable<TreeNode> GetChildren()
{
return This.Nodes;
}
public static implicit operator ITreeNodeExtn(TreeNode tn)
{
return new TreeNodeExtn { This=tn };
}
public static implicit operator TreeNode(TreeNodeExtn tn)
{
return tn.This;
}
}
// Lots of other nice stull you can do with the above... but thats for another day.
Some syntactic sugar?
Instead of code like:
{
using (var a=new c())
{
using(var b=new d())
{
.....
}
}
}
why not simply allow the scope to be implied by the enclosing braces?
{
var x = new myglocal();
local var a = new c() ;
local var b = new d();
}
Also how about a thread-safe null-checking dot operator .? to supplement the
?? operator.
so
var x= (a!=null)?a.fn():null
can be shortend to
var x = a.?fn();
For value-types the return value could be up-converted to a nullbable type as required.
int v = a?.MyIntFn() ?? mydefaultValue;
Invoking events/delegates could also be supported as folows
myevent.?(sended,args);
Removing one of the often -discussed current problems of how to achive this safely.