C# 无法将方法转换为非委托类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14877355/
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
C# cannot convert method to non delegate type
提问by gts13
I have a class called Pin
.
我有一个名为Pin
.
public class Pin
{
private string title;
public Pin() { }
public setTitle(string title) {
this.title = title;
}
public String getTitle()
{
return title;
}
}
From another class I add Pins objects in a List<Pin>
pins and from another I want to iterate the List pins and get the elements. So I have this code.
从另一个类中,我将 Pins 对象添加到List<Pin>
pin 中,从另一个类中我想迭代 List pin 并获取元素。所以我有这个代码。
foreach (Pin obj in ClassListPin.pins)
{
string t = obj.getTitle;
}
With this code I cannot retrieve the title. Why?
使用此代码,我无法检索标题。为什么?
(Note: ClassListPin
is just a static class which contains some elements and one of these, is the List<Pin>
pins)
(注意:ClassListPin
只是一个包含一些元素的静态类,其中一个是List<Pin>
引脚)
采纳答案by antonijn
You need to add parentheses after a method call, else the compiler will think you're talking about the method itself (a delegate type), whereas you're actually talking about the return value of that method.
您需要在方法调用后添加括号,否则编译器会认为您在谈论方法本身(委托类型),而实际上您在谈论该方法的返回值。
string t = obj.getTitle();
Extra Non-Essential Information
额外的非必要信息
Also, have a look at properties. That way you could use title as if it were a variable, while, internally, it works like a function. That way you don't have to write the functions getTitle()
and setTitle(string value)
, but you could do it like this:
另外,看看属性。这样你就可以把 title 当作一个变量来使用,而在内部,它就像一个函数。这样你就不必编写函数getTitle()
and 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
}
}
Or you could use auto-implemented properties, which would use this by default:
或者您可以使用自动实现的属性,默认情况下会使用它:
public string Title { get; set; }
And you wouldn't have to create your own backing field (_title
), the compiler would create it itself.
而且您不必创建自己的支持字段 ( _title
),编译器会自行创建它。
Also, you can change access levels for property accessors (getters and setters):
此外,您可以更改属性访问器(getter 和 setter)的访问级别:
public string Title { get; private set; }
You use properties as if they were fields, i.e.:
您可以像使用字段一样使用属性,即:
this.Title = "Example";
string local = this.Title;
回答by Lews Therin
Because getTitle
is not a string
, it returns a reference or delegate
to a method (if you like), if you don't explicitly callthe method.
因为getTitle
不是 a string
,它返回一个引用或delegate
一个方法(如果你喜欢),如果你没有显式调用该方法。
Call your method this way:
以这种方式调用您的方法:
string t= obj.getTitle() ; //obj.getTitle() says return the title string object
However, this would work:
但是,这会起作用:
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();`
回答by Andre Loker
To execute a method you need to add parentheses, even if the method does not take arguments.
要执行一个方法,您需要添加括号,即使该方法不带参数。
So it should be:
所以应该是:
string t = obj.getTitle();
回答by Bobson
getTitle
is a function, so you need to put ()
after it.
getTitle
是一个函数,所以你需要把()
它放在后面。
string t = obj.getTitle();
回答by eandersson
As mentioned you need to use obj.getTile()
如前所述,您需要使用 obj.getTile()
But, in this case I think you are looking to use a Property.
但是,在这种情况下,我认为您正在寻找使用Property。
public class Pin
{
private string title;
public Pin() { }
public setTitle(string title) {
this.title = title;
}
public String Title
{
get { return title; }
}
}
This will allow you to use
这将允许您使用
foreach (Pin obj in ClassListPin.pins)
{
string t = obj.Title;
}
回答by Sergey Berezovskiy
As @Antonijn stated, you need to executegetTitle method, by adding parentheses:
正如@Antonijn 所说,您需要通过添加括号来执行getTitle 方法:
string t = obj.getTitle();
But I want to add, that you are doing Java programming in C#. There is concept of properties(pair of get and set methods), which should be used in such cases:
但我想补充一点,您正在用 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; }
}
}
And even more, in this case you can ask compiler not only for get and set methods generation, but also for back storage generation, via auto-impelemented propertyusage:
更重要的是,在这种情况下,您不仅可以要求编译器生成 get 和 set 方法,还可以通过自动引入的属性使用生成后备存储:
public class Pin
{
public string Title { get; set; }
}
And now you don't need to executemethod, because properties used like fields:
现在你不需要执行方法,因为属性像字段一样使用:
foreach (Pin obj in ClassListPin.pins)
{
string t = obj.Title;
}
回答by legrandviking
You can simplify your class code to this below and it will work as is but if you want to make your example work, add parenthesis at the end : string x = getTitle();
您可以将您的类代码简化为下面的代码,它将按原样工作,但如果您想让示例工作,请在末尾添加括号: string x = getTitle();
public class Pin
{
public string Title { get; set;}
}