Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CS0102 The type 'A' already contains a definition for 'set_color'

Tags:

c#

.net

I just came across the above error while I was trying to compile a proxy class generated through svcutil. Here's a short version of the problem:

class A
{
    private string colorField;
    private string set_colorField; 

    public string color
    {
        get
        {
            return this.colorField;
        }
        set
        {
            this.colorField = value;
        }
    }

    public string set_color
    {
        get
        {
            return this.set_colorField;
        }
        set
        {
            this.set_colorField = value;
        }
    }
}

This compiles fine:

public string Color
{
    get;set;
}

public string Set_Color
{
    get;set;
}

But this throws the same error:

public string color
{
    get;set;
}

public string set_color
{
    get;set;
}

I don't recall ever reading about this restriction. Can someone point me to the relevant section of the C# compiler spec?

like image 375
Icarus Avatar asked Oct 23 '17 21:10

Icarus


1 Answers

https://github.com/dotnet/csharplang/blob/master/spec/classes.md#properties

Member names reserved for properties

For a property P (Properties) of type T, the following signatures are reserved:

T get_P();
void set_P(T value);

In the case where you have color property, set_color(...) is reserved and that's why you can't have set_color property too, since it tries to compile to the same signature.

In the case where you have Color property, set_Color(...) is reserved for it and that's why Set_Color (note the Capital letter) works.

like image 176
hyankov Avatar answered Oct 30 '22 12:10

hyankov