C#无法将方法转换为非委托类型

40
我有一个名为Pin的类。
public class Pin
{
    private string title;

    public Pin() { }

    public setTitle(string title) {
        this.title = title;
    }
    public String getTitle()
    {
        return title;
    }
}

我从另一个类中将Pins对象添加到名为List<Pin>的列表pins中,然后我想要从另一个类中迭代List pins并获取其中的元素。所以我有这段代码。

foreach (Pin obj in ClassListPin.pins)
{
     string t = obj.getTitle;
}

使用这段代码,我无法获取标题。为什么?

(注意:ClassListPin只是一个包含一些元素和其中之一的List<Pin> pins的静态类)


1
因为你需要调用它,例如 obj.getTitle() - leppie
1
你尝试过在谷歌上搜索吗?这是一个相当常见的初学者错误。 - antonijn
1
有点偏题,但属性被引入来代替编写setter和getter。更多信息请参考:http://msdn.microsoft.com/en-us/library/x9fsa0sw%28v=vs.80%29.aspx - Matthew
1
我承认那是一个愚蠢的错误... - gts13
7个回答

74

在调用方法后,需要加上括号,否则编译器会认为你正在谈论该方法本身(一个委托类型),而实际上你是在谈论该方法的返回值。

string t = obj.getTitle();

额外的非必要信息

同时,查看属性。这样,您可以像使用变量一样使用title,而在内部它起作用如同函数。这样,您不必编写 getTitle()setTitle(string value) 函数,而是可以像这样操作:

public string Title // Note: public fields, methods and properties use PascalCasing
{
    get // This replaces your getTitle method
    {
        return _title; // Where _title is a field somewhere
    }
    set // And this replaces your setTitle method
    {
        _title = value; // value behaves like a method parameter
    }
}

或者你可以使用自动实现的属性,它们默认使用此方法:

public string Title { get; set; }

你不需要创建自己的后备字段(_title),编译器会自动创建。

此外,你可以更改属性访问器(getter和setter)的访问级别:

public string Title { get; private set; }

您可以像使用字段一样使用属性,例如:
this.Title = "Example";
string local = this.Title;

4
哦,天啊…那是一个幼稚的错误! - gts13

8

正如@Antonijn所述,您需要添加括号执行getTitle方法:

 string t = obj.getTitle();

但我想补充一点,你正在使用C#进行Java编程。在这种情况下,应该使用属性的概念(即get和set方法对),以便更好地完成任务:

public class Pin
{
    private string _title;

    // you don't need to define empty constructor
    // public Pin() { }

    public string Title 
    {
        get { return _title; }
        set { _title = value; }
    }  
}

而且,在此情况下,您不仅可以要求编译器生成get和set方法,还可以通过自动实现属性来生成后端存储:

public class Pin
{
    public string Title { get; set; }
}

现在您无需执行方法,因为属性可以像字段一样使用:

foreach (Pin obj in ClassListPin.pins)
{
     string t = obj.Title;
}

8

getTitle是一个函数,所以您需要在它后面加上()

string t = obj.getTitle();

6

正如所提到的,您需要使用obj.getTile()

但是,在这种情况下,我认为您要使用一个属性

public class Pin
{
    private string title;

    public Pin() { }

    public setTitle(string title) {
        this.title = title;
    }

    public String Title
    {
        get { return title; }
    }
}

这将允许您使用

foreach (Pin obj in ClassListPin.pins)
{
     string t = obj.Title;
}

1
应该是 Title,而不是 getTitle - antonijn

6

你可以简化你的类代码如下,它会正常运行,但如果你想让你的示例起作用,请在结尾处添加括号:string x = getTitle();

public class Pin
{
   public string Title { get; set;}
}

4
因为`getTitle`不是一个`string`类型,所以如果您没有显式地调用方法,它将返回一个对方法的引用或者委托(如果您喜欢的话)。
请按照以下方式调用您的方法:
string t= obj.getTitle() ; //obj.getTitle()  says return the title string object

然而,这个可以实现:
Func<string> method = obj.getTitle; // this compiles to a delegate and points to the method

string s = method();//call the delegate or using this syntax `method.Invoke();`

3

为了执行一个方法,即使该方法不需要参数,也需要加上括号。

所以应该是:

string t = obj.getTitle();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接