-4

I want to extend a property to a class such as 'string class'.

e.g.:

    public Color TextColor
    {
        get { ... }
        set { ... }
    }

    string name="reza";
    name.TextColor;
    or
    name.TextColor= Color.Red;
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
Reza Mahmoodi
  • 170
  • 1
  • 5
  • 2
    Extension properties are not supported at this time. Only extension methods. More info [here](https://roslyn.codeplex.com/discussions/541454). – D Stanley Sep 03 '14 at 04:09
  • @DStanley They are supported in WPF via [Attached Properties](http://msdn.microsoft.com/en-us/library/ms749011%28v=vs.110%29.aspx). To the OP, because you are talking about text color, is this for WPF or something else? – Scott Chamberlain Sep 03 '14 at 04:11
  • 2
    @ScottChamberlain, not really. That's just a XAML concept. In VB or C# code you can't access the attached property as though it was a member of the target object. You can't write C# code that looks anything like what the OP has suggested. – jmcilhinney Sep 03 '14 at 04:15
  • possible duplicate of [Does C# have extension properties?](http://stackoverflow.com/questions/619033/does-c-sharp-have-extension-properties) – Sam Sep 03 '14 at 05:02

1 Answers1

2

You cannot (at the time of writing) extend properties in c#, and you certainly cannot extend string seeing as it is a sealed class. Use methods instead of properties, and make a StringExtension class instead of inheriting string.

Assaf
  • 1,352
  • 10
  • 19