成员名称不能与其封闭类型 C# 相同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10070701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:20:36  来源:igfitidea点击:

member names cannot be the same as their enclosing type C#

c#

提问by SHRI

The code below is in C# and I'm using Visual Studio 2010.

下面的代码使用 C#,我使用的是 Visual Studio 2010。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace FrontEnd
{
    class Flow
    {
        long i;
        private int x,y;
        public int X
        {
            get;set;
        }
        public int Y
        {
            get;set;
        }

        private void Flow()
        {
            X = x;
            Y = y;
        }

        public void NaturalNumbers(int x, int y)
        {
            for (i = 0; i < 9999; i++)
            {
                Console.WriteLine(i);
            }
            MessageBox.Show("done");
        }
    }
}

When I compile the above code I get this error:

当我编译上面的代码时,我收到此错误:

Error: 'Flow': member names cannot be the same as their enclosing type

错误:“流程”:成员名称不能与其封闭类型相同

Why? How can I resolve this?

为什么?我该如何解决这个问题?

采纳答案by mshsayem

Method names which are same as the class name are called constructors. Constructors do not have a return type. So correct as:

与类名相同的方法名称为构造函数。构造函数没有返回类型。所以正确为:

private Flow()
{
   X = x;
   Y = y;
}

Or rename the function as:

或将函数重命名为:

private void DoFlow()
{
   X = x;
   Y = y;
}

Though the whole code does not make any sense to me.

虽然整个代码对我来说没有任何意义。

回答by Wouter de Kort

The problem is with the method:

问题在于方法:

private void Flow()
{
    X = x;
    Y = y;
}

Your class is named Flowso this method can't also be named Flow. You will have to change the name of the Flowmethod to something else to make this code compile.

您的类已命名,Flow因此此方法不能也命名为Flow。您必须将Flow方法的名称更改为其他名称才能编译此代码。

Or did you mean to create a private constructor to initialize your class? If that's the case, you will have to remove the voidkeyword to let the compiler know that your declaring a constructor.

或者你的意思是创建一个私有构造函数来初始化你的类?如果是这种情况,您将不得不删除void关键字以让编译器知道您声明了一个构造函数。

回答by Subhranshu

A constructor should no have a return type . remove void before each constructor .

构造函数不应该有返回类型。在每个构造函数之前删除 void 。

Some very basic characteristic of a constructor :

构造函数的一些非常基本的特征:

a. Same name as class b. no return type. c. will be called every time an object is made with the class. for eg- in your program if u made two objects of Flow, Flow flow1=new Flow(); Flow flow2=new Flow(); then Flow constructor will be called for 2 times.

一种。与 b 类同名。没有返回类型。C。每次使用该类创建对象时都会调用。例如 - 在你的程序中,如果你创建了两个 Flow 对象,Flow flow1=new Flow(); Flow flow2=new Flow(); 然后 Flow 构造函数将被调用 2 次。

d. If you want to call the constructor just for once then declare that as static (static constructor) and dont forget to remove any access modifier from static constructor ..

d. 如果您只想调用一次构造函数,则将其声明为静态(静态构造函数)并且不要忘记从静态构造函数中删除任何访问修饰符..

回答by Hoby

just remove this because constructor don't have a return type like void it will be like this :

只需删除它,因为构造函数没有像 void 这样的返回类型,它将是这样的:

private Flow()
    {
        X = x;
        Y = y;
    }  

回答by Qasim Khan

Constructors don't return a type , just remove the return type which is void in your case. It would run fine then.

构造函数不返回 type ,只需删除在您的情况下为 void 的返回类型。那时它会运行良好。

回答by Kunj Sangani

As Constructor should be at the starting of the Class , you are facing the above issue . So, you can either change the name or if you want to use it as a constructor just copy the method at the beginning of the class.

由于 Constructor 应该在 Class 的开头,您正面临上述问题。因此,您可以更改名称,或者如果您想将其用作构造函数,只需在类的开头复制该方法。