该类型已包含定义

时间:2022-01-15 17:08:56

I got a problem in this program,

我在这个程序中遇到了问题,

namespace WorkingWithXML
{
    public class information
    {
        private string Data1;
        private string Data2;
        private string Data3;

        public string Data1
        {
            get { return Data1; }
            set { Data1 = value; }
        }
        public string Data2
        {
            get { return Data2; }
            set { Data2 = value; }
        }
        public string Data3
        {
            get { return Data3; }
            set { Data3 = value; }
        }
    }
}

The type 'WorkingWithXML.information' already contains a definition for 'data1'

类型'WorkingWithXML.information'已包含'data1'的定义

can any one guess whats the error thanks..!

任何人都可以猜测错误是什么感谢..!

3 个解决方案

#1


You use the same name for a field and a property in the same class - two Data1, Data2 Data3. It is not allowed.

对同一个类中的字段和属性使用相同的名称 - 两个Data1,Data2 Data3。这个不允许。

I'l suggest you swith the field names to data1,data2,data3.

我建议你将字段名称转换为data1,data2,data3。

#2


You've got a field and a property with the same name. On a setter like this:

你有一个字段和一个同名的属性。在这样的二传手:

set { Data1 = value; }

The compiler can't tell if you're referring to the member variable or the property (which would be a recursive set and cause a stack overflow exception).

编译器无法判断您是在引用成员变量还是属性(这将是一个递归集并导致堆栈溢出异常)。

The normal approach is to name member variables differently. For example either _Data1 or m_Data1, for example.

通常的方法是以不同方式命名成员变量。例如,_Data1或m_Data1。

#3


you got mistake in implementation properties, you should use backfields or rewrite to auto properties

如果您在实现属性中出错,则应使用后场或重写为自动属性

namespace WorkingWithXML
{
    public class information
    {
        public string Data1 { get; set; }
        public string Data2 { get; set; }
        public string Data3 { get; set; }
    }
}

#1


You use the same name for a field and a property in the same class - two Data1, Data2 Data3. It is not allowed.

对同一个类中的字段和属性使用相同的名称 - 两个Data1,Data2 Data3。这个不允许。

I'l suggest you swith the field names to data1,data2,data3.

我建议你将字段名称转换为data1,data2,data3。

#2


You've got a field and a property with the same name. On a setter like this:

你有一个字段和一个同名的属性。在这样的二传手:

set { Data1 = value; }

The compiler can't tell if you're referring to the member variable or the property (which would be a recursive set and cause a stack overflow exception).

编译器无法判断您是在引用成员变量还是属性(这将是一个递归集并导致堆栈溢出异常)。

The normal approach is to name member variables differently. For example either _Data1 or m_Data1, for example.

通常的方法是以不同方式命名成员变量。例如,_Data1或m_Data1。

#3


you got mistake in implementation properties, you should use backfields or rewrite to auto properties

如果您在实现属性中出错,则应使用后场或重写为自动属性

namespace WorkingWithXML
{
    public class information
    {
        public string Data1 { get; set; }
        public string Data2 { get; set; }
        public string Data3 { get; set; }
    }
}