Welcome to MSDN Blogs Sign in | Join | Help

Hello Web

Who am I?

My name is Sreekar Choudhary and Welcome to my blog, I am a Dev on the C# compiler Team. I work most of the time on language features and implementing debugging framework for C# developers inside VS.

 

What's this blog about?

Well it’s about the cool features of that I get to design and work on and any other relavent technical topic that is of interest to me or the readers.

 

Published Tuesday, April 03, 2007 5:47 PM by Sree_c

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Hello Web

Monday, April 16, 2007 4:08 AM by Stefan Wenig

Hi Sree,

When pointing to your blog, Eric Lippert mentioned that you're the expert for extension methods. What do you think about the following code:

namespace MyCollections

{

 public interface IMyCollection

 {

 }

 public static class MyCollectionUtility

 {

   public static void Foo(this IMyCollection collection)

   {

   }

 }

}

namespace OtherNamespace

{

 // using MyCollections;

 public static class CollectionTest

 {      

   public static void Test()

   {

     MyCollections.IMyCollection collection = null;

     collection.Foo();

   }

 }

}

I have to uncomment "using MyCollections;" to make it work, but I think I should not have to. If extension methods would be included not only from the namespaces specified via "using", but also from the namespace of the target type (and its supertypes), we would finally be able to specify stateless default methods for interfaces!

During a past chalk talk, anders heijlsberg said that this kind of methods would be considered for future releases:

public interface IWhatever

{

 DoSomething (obj o);

 DoSomething ()

 {

   DoSomething (null);

 }

}

I guess since extension methods allow almost the same scenario, the likeliness of this feature was set back quite a bit with the announcement of extension methods in c# 3. However, for this usage scenario, having no access to the methods without "using" the namespace, this would be a rather incomplete replacement.

(That said, you might want to automatically include only extension methods from the same namespace AND assembly, so people are not tempted to inject their stuff into someone elses namespace, like System.*)

# Community Convergence XXV

Saturday, April 21, 2007 1:09 PM by Charlie Calvert's Community Blog

Visual Studio Orcas Beta 1 is available for download . Though quite similar to the March CTP in terms

# re: Hello Web

Wednesday, April 25, 2007 5:54 PM by Sree_c

I think i am a little confused here, so let me repeat what you are saying.

You would like the extension methods to be imported from the namespace that declares the type used in the "instance parameter".

Well this should be done for you automatically since the namespace that decared the type must be imported before it can be used ( and so will be extension methods).

Looking at your example the code could possibly not comile with out the using since we would not know how to find the defintion of

public interface IMyCollection

with out it. ( at the same time we will import the extensions)

Hope this helps... do let me know if i am missing something

Sree

# re: Hello Web

Thursday, April 26, 2007 3:48 AM by Stefan Wenig

Hi Sree

OK, here's my point: I can use a type without either "using" it or specifying it directly by using the return value of some other type's member.

namespace Namespace1

{

 class Class1 {

   public Namespace2.Iface2 Iface2 { get; }

}

namespace Namespace2

{

 interface Iface2 {

   string Foo (obj o) {...}

 }

 static class Iface2Extensions {

   string Foo (this Iface2 iface2) {

     return iface2.Foo (null);

   }

 }

}

-------------

using Namespace1;

main() {

 Class1 c;

 c.Class2.Foo(null); // OK

 c.Class2.Foo();     // won't work without "using"

at this point i have not mentioned Namespace2 in my code at all, still i get access to every method of Iface2. i believe the same should apply to extension methods from Namespace2. This would make extension methods for interfaces consistent with the original idea of default interface implementations and probably reduce confusion. Hope this was clearer.

Thanks, Stefan

# re: Hello Web

Thursday, April 26, 2007 3:58 AM by Stefan Wenig

btw, of course the method in my first post has the same issue: fully qualifying the interface name instead of "using" the namespace will not give you it's extension methods either. this is probably just a less common scenario that return values from other types, so i wanted to point out the latter too.

stefan

# re: Hello Web

Thursday, April 26, 2007 2:06 PM by Sree_c

Cool Stefan,

I can totally see what you are saying. I am not aware of any plans to change the lookup rules for extension methods as of now.

Though this would make the lookup for extension methods a whole lot costlier and this would certainly be a breaking change for exiting code.

Its a cool feature though i see a lot of side effects.. let me think on this for a while and i will post back soon.

# re: Hello Web

Friday, April 27, 2007 3:39 AM by Stefan Wenig

Sree,

breaking changes is exactly the reason why i think you should consider it for c# 3.0 RTM. right now, there are no extension methods, and since instance methods always have precedence over extension methods, there should be no breaking changes concerning existing c# 2.0 code. once c# 3.0 is out of the house however, you'd probably have a hard time introducing that change, since users could no longer override existing extension methods from the type's namespace by omitting the using statement for that namespace. (than again, this is probably a very unlikely scenario)

thanks for considering this!

stefan

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker