CS0102类型“ A”已经包含“ set_color”的定义 [英] CS0102 The type 'A' already contains a definition for 'set_color'

查看:620
本文介绍了CS0102类型“ A”已经包含“ set_color”的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译通过svcutil生成的代理类时遇到了以上错误。这是问题的简短版本:

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;
        }
    }
}

这可以很好地编译:

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;
}

我从没有回想过有关此限制的内容。有人可以指出我C#编译器规范的相关部分吗?

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

推荐答案

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

为属性保留的成员名称

对于类型T的属性P(属性),以下签名保留

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



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

如果您拥有 color 属性, set_color(...)被保留,这就是为什么您也不能拥有 set_color 属性的原因,因为它会尝试

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.

如果您具有 Color 属性,则 set_Color(...)为此保留,这就是为什么 Set_Color (请注意大写字母)起作用的原因。

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.

这篇关于CS0102类型“ A”已经包含“ set_color”的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆