0

I would like to extend the string object and have those extensions part of a nested class, however directly doing it this way :

public static class StringExtensions
{
    public static class Patterns
    {
        public static string NumbersOnly(this string s)
        {
            return new String(s.Where(Char.IsDigit).ToArray());
        }
    }
}

... gives the error as stated for the title of this post.

How can I write this differently so that when I call it, it can be called like this :

string s = "abcd1234";
s = s.Patterns.NumbersOnly();

I know I can move NumbersOnly as a direct child of StringExtensions to make the error go away, however my intention is to organize the methods into categories which will have a lot of methods. In this example, NumbersOnly is just one of about 40 pattern matches I intend to have there and I do not wish to clutter of the root of the object with methods like: PatternNumbersOnly or NumbersOnly etc.

Note: This question is different than questions like this one as I am not asking why this problem exists, I am looking for a workaround so that I can have the functionality or similar functionality which the reason of this error is denying me from.

Community
  • 1
  • 1
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • This would work in swift. The c# compiler team has looked into it and decided that it doesn't really add up much value to what it costs, so they will not be implementing it (atleast not in the close future) – Mafii Jul 19 '16 at 15:47
  • @Mafii - could you provide a source to validate your statement please. – Kraang Prime Jul 28 '16 at 23:58
  • http://stackoverflow.com/a/619047/5962841 – Mafii Jul 29 '16 at 03:55
  • "There is an extension members item in theC# 7 work list so it may be supported in the near future. The current status of extension property can be found on Github under the related item. However, there is an even more promising topic which is the "extend everything" with a focus on especially properties and static classes or even fields." – Mafii Jul 29 '16 at 03:58

1 Answers1

5

You can't - there are no "extension properties".

The best you can get - s.Patterns().NumbersOnly() by introducing intermediate class to return from Patterns extension method.

Sample puts all methods in single class, but you can organize them any way you want to different classes as long as extensions methods satisfy "defined in a top level static class":

public static class StringExtensions
{
  public class PatternsX
  { 
    public string Value {get;set;}
  }

  public static PatternsX Patterns(this string s)
  {
    return new PatternsX { Value = s};
  }

  public static string NumbersOnly(this PatternsX s)
  {
    return new String(s.Value.Where(Char.IsDigit).ToArray());
  }
}

....    
Console.WriteLine("123ver".Patterns().NumbersOnly()); // results in 123
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 2
    Alternatively, `NumbersOnly` can just be an instance method of `PatternsX`. –  Jul 16 '16 at 20:17
  • I see what you did, basically mapped an instance of itself to the `.Patterns` object. Not ideal, for memory conservation, so I guess will just leave with a non reflected instance of the methods in the root of the `string` object. Marking this as the correct solution and upvoting. I believe you are correct with this. Hopefully c# 7 adopts better organization in extensibility. Just happy to be able to have the framework inherit my code :) – Kraang Prime Jul 17 '16 at 00:02