none
Static extension methods

    Question

  • Hi,

    Anyone knows if static extension methods are on their way for C# 4.0? So people could have methods like Color.ToPoint3, etc.





    Thanks,
    Aw
    Sunday, March 02, 2008 1:28 PM

Answers

  •  Azurewrath wrote:
    So no one knows about it?




    Thanks,
    Aw
    Nothing has formally been announced as to what will be in the next version of C#.  There's been many discussions about what it will contain, static extension methods is one thing that has been discussed.
    Thursday, March 06, 2008 8:28 PM
    Moderator
  • ...I was on a much needed vacation...

     

    Static extension methods have been discussed publicly here:  http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2046738&SiteID=1. I know of no public "static extension method" stance from anyone at Microsoft.  Maybe you can broach the topic here: http://code.msdn.microsoft.com/vslangfutures

     

    Extension operators are a different, related, topic that has been mentioned before as well.  I would hope any static extension method support would also include operators.

     

     

     

    Friday, March 07, 2008 3:26 PM
    Moderator

All replies

  • Explain what you are asking for ? what do you want vto achieve via color.ToPoint3() ??
    Sunday, March 02, 2008 10:06 PM
  • Are you refering to the ability to extend an existing class with new methods without having access to the original source?  For example, extending String by adding a LeftOf method would require this:

     

    Code Snippet

    public static class StringHelper
    {

       public static string LeftOf ( string value, int count ) { ... }
    }

     

    You could then do this:

     

    Code Snippet

    string str = "Hello World";

    string str2 = StringHelper.LeftOf(str, 5);

     

    This is nice, but inconvenient since you really just want to deal with the string.  Ideally you'd like to do this:

     

    Code Snippet

    string str = "Hello World";

    string str2 = str.LeftOf(str, 5);

     

    Since you don't have access to the String source and it is sealed you can't add the method.  With extension methods (available in .NET v3.5+) you can extend the class without modifying the original source.  You would continue to use a static class but you modify the first parameter to include the this keyword.  This marks the method as an extension method.

     

    Code Snippet
    public static class StringHelper
    {
       public static string LeftOf ( this string value, int count ) { ... }
    }

     

    With an extension method you are back to the usage case you like.  The compiler ultimately translates the call into the standard static method call.  You can confirm this by looking at the IL.  This feature is strictly a compiler feature.

     

    Michael Taylor - 3/2/08

    http://p3net.mvps.org

     

    Monday, March 03, 2008 12:15 AM
    Moderator
  • Thanks for replying.

    I know about extension methods that lets me to use stuff like:

    myColor.Mymethod

    But I want the ability to do stuff like:

    Color.Mix(Color c1, Color c2)

    instead of:

    c1.Mix(c2)


    I remember someone referring this to have a chance to be included in C3 4, but was wondering if this is happening?




    Thanks,
    Aw
    Monday, March 03, 2008 3:32 AM
  • So no one knows about it?




    Thanks,
    Aw
    Monday, March 03, 2008 7:38 PM
  • I think I heard mention of such a thing back when they were really talking about extension methods but I haven't heard anything since.  I don't find static extensions that useful myself.  Even if they are looking into adding it there isn't really anything different you'd do today to prepare for it.  Maybe somebody on the C# compiler team can give you more information.  There have been only a few "planned" features for the next version released so far. 

     

    Michael Taylor - 3/3/08

    http://p3net.mvps.org

     

     

    Monday, March 03, 2008 8:09 PM
    Moderator
  •  Azurewrath wrote:
    Thanks for replying.

    I know about extension methods that lets me to use stuff like:

    myColor.Mymethod

    But I want the ability to do stuff like:

    Color.Mix(Color c1, Color c2)

    instead of:

    c1.Mix(c2)


    I remember someone referring this to have a chance to be included in C3 4, but was wondering if this is happening?




    Thanks,
    Aw


    Hi,

    Why don't you create a class named ColorMixer and add a static Mix() method to it? Is there any particular reason behind your need to add static extension method to the Color class itself, and not create another class to do it? Because it is a good practice to share responsibilities between different classes, so let the Color class store color details and ColorMixer mix two colors.

    I hope this helps.
    Monday, March 03, 2008 8:33 PM
  • Thanks currently I do that, but sometimes you really need to add to the build in classes. Like consider this case:

    Point3.FindNearestPlane()

    This method must reside in Point3, but if I can't do that, I will have to use something like:

    MyPoint3.FindNearestPlane()

    Also isn't it the same thing with instance extension methods? We don't really need them, one can perfect use a custom class to do what he/she wants with the object, but they put that feature in, just for clarity, right?





    Thanks again,
    Aw
    Monday, March 03, 2008 9:08 PM
  • Ya I get your point and I am surprized to know that we can not extend the class with static methods. I felt the same thing when I faced the issue with static properties in interfaces.

    I hope someone comes up with the better solution.
    Tuesday, March 04, 2008 8:43 PM
  • Thanks Jay. That makes it very limiting IMO. Why not just give us the ability and people who don't want to use it (like the people who don't want to use instance extension methods) wouldn't use it.

    I think for clarity having both is best, in that some methods don't make much sense with instance extension methods. Stuff like this:


    v3a.Matrix4FromVector3(v3b)

    where as you could say better:

    Vector3.Matrix4FromVector3(v3a, v3b)





    Thanks,
    Aw
    Wednesday, March 05, 2008 7:50 PM
  • True ..very much true. But is something like is expected in next/future releases of c# ? anyone tried this in VS2k8??
    Thursday, March 06, 2008 12:46 AM
  • You can't do it in C# 3. I hope we can do it in v4.




    Aw
    Thursday, March 06, 2008 7:21 PM
  •  Azurewrath wrote:
    So no one knows about it?




    Thanks,
    Aw
    Nothing has formally been announced as to what will be in the next version of C#.  There's been many discussions about what it will contain, static extension methods is one thing that has been discussed.
    Thursday, March 06, 2008 8:28 PM
    Moderator
  • Hey Peter,

    You are back! Haven't seen you here for some time.

    Btw where did you read what has been discussed, or were you in the discussions?

    If static extension methods is included, does it mean we can overload operators for existing classes, like Color? That would be so great, that I wouldn't want anything else. I am very picky into adding what should be added. So I wouldn't overload it unnecessarily to make things very unreadable.




    Thanks again,
    Aw
    Friday, March 07, 2008 12:45 AM
  • ...I was on a much needed vacation...

     

    Static extension methods have been discussed publicly here:  http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2046738&SiteID=1. I know of no public "static extension method" stance from anyone at Microsoft.  Maybe you can broach the topic here: http://code.msdn.microsoft.com/vslangfutures

     

    Extension operators are a different, related, topic that has been mentioned before as well.  I would hope any static extension method support would also include operators.

     

     

     

    Friday, March 07, 2008 3:26 PM
    Moderator
  • Glad to hear you had some time off. Smile

    Thanks for the links. I hope they make it to C# 4.




    Cheers,
    Aw
    Friday, March 07, 2008 8:22 PM
  • You can use Extension methods to extend existing types
    Please see below link for more details

    http://methodextension.blogspot.in/


    Naren

    Sunday, April 22, 2012 6:48 AM
  • Please don't revive old threads just to promote your own blog.
    Monday, April 23, 2012 9:11 AM
  • Static extensions to existing classes would be extremely useful in a number of cases, especially in static classes, structures and enumerations.

    For example I would love to extend the unit testing assertion static class like so:

    namespace AmpCms.QA.Extensions

    {

        using System;

        using System.Diagnostics.Contracts;

        using System.Globalization;

        using Microsoft.VisualStudio.TestTools.UnitTesting;

        public static class AssertExtensions

        {

            public static void StartsWidth<TAssert>(string startText, string actual, bool ignoreCase, CultureInfo culture, string message) where TAssert : Assert

            {

                Contract.Requires(!string.IsNullOrWhiteSpace(startText));

                Assert.AreEqual(startText, actual.Substring(0, startText.Length), ignoreCase, culture, message);

            }

        }

    }

    So that I can have a test assertion such as:

    Assert.StartsWidth("xxx", testString, false, CultureInfo.InvariantCulture, "Does not start with text 'xxx'");

    Of course in this example I am just making up the syntax that could be used to identify the class for the extension by borrowing from generics, this may not be the best way to code this.

    If we had this capability, many would love to extend other classes such as Enum to define a method such as “GetDictionaryValues” that will provide a “Dictionary<int, string>” containing all the values in an enumeration.

    Although it is true you can write your own class, extensions provide much better cohesion in the code.


    Software Engineer

    Monday, January 06, 2014 8:03 PM