C#返回字符串方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11317587/
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# Return a string method
提问by user1462498
I am trying to return a string from the SalesPersonobject with fullNameMethodto the main program, but this isn't working. What am I doing wrong?
我试图从SalesPerson对象返回一个字符串fullNameMethod到主程序,但这不起作用。我究竟做错了什么?
class SalesPerson
{
string firstName, lastName;
public string FirstName { get { return firstName; } set { firstName = value; } }
public string LastName { get { return lastName; } set { lastName = value; } }
public SalesPerson(string fName, string lName)
{
firstName = fName;
lastName = lName;
}
public string fullNameMethod()
{
string x = firstName + " " + lastName;
return x;
}
}
class Program
{
static void Main(string[] args)
{
SalesPerson x = new SalesPerson("john", "Doe");
Console.WriteLine("{0}",x.fullNameMethod);
}
}
采纳答案by John Mitchell
Your currently trying to access a method like a property
您当前正在尝试访问诸如属性之类的方法
Console.WriteLine("{0}",x.fullNameMethod);
Should be
应该
Console.WriteLine("{0}",x.fullNameMethod());
Alternatively you could turn it into a proprty using
或者,您可以使用
public string fullName
{
get
{
string x = firstName + " " + lastName;
return x;
}
}
回答by Lou Franco
Use x.fullNameMethod()to call the method
使用x.fullNameMethod()要调用的方法
回答by Christofer Eliasson
You don't have to have a method for that, you could create a property like this instead:
你不必有一个方法,你可以创建一个这样的属性:
class SalesPerson
{
string firstName, lastName;
public string FirstName { get { return firstName; } set { firstName = value; } }
public string LastName { get { return lastName; } set { lastName = value; } }
public string FullName { get { return this.FirstName + " " + this.LastName; } }
}
The class could even be shortened to:
该类甚至可以缩短为:
class SalesPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName {
get { return this.FirstName + " " + this.LastName; }
}
}
The property could then be access like any other property:
然后可以像访问任何其他属性一样访问该属性:
class Program
{
static void Main(string[] args)
{
SalesPerson x = new SalesPerson("John", "Doe");
Console.WriteLine(x.FullName); // Will print John Doe
}
}
回答by Serguei Fedorov
You forgot the () at the end. It is not a variable but a function and when there are not parameters, you still need the () at the end
你忘记了 () 最后。它不是一个变量而是一个函数,当没有参数时,你仍然需要最后的 ()
For future coding practices, I would highly recommend reforming the code a little bit this is can become frustrating to read:
对于未来的编码实践,我强烈建议对代码进行一点改革,这可能会令人沮丧:
public string LastName
{ get { return lastName; } set { lastName = value; } }
If there is any kind of processing which happens in here, thankfully doesn't happen here, it will become very confusing. If your going to pass your code onto someone else, I would recommend:
如果这里发生了任何类型的处理,幸好这里没有发生,它会变得非常混乱。如果您要将代码传递给其他人,我建议:
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
Its a lot longer but its much easier to read when glancing at huge section of code.
它更长,但在浏览大量代码时更容易阅读。
回答by Joyce Kai
These answers are all WAY too complicated! The way he wrote the method is fine. The problem is where he invoked the method. He did not include parentheses after the method name, so the compiler thought he was trying to get a value from a variable instead of a method. In VB and Delphi, those parentheses are optional, but in C#, they are required. So, to correct the last line of the original post, Console.WriteLine("{0}",x.fullNameMethod());
这些答案都太复杂了!他写方法的方式很好。问题是他在哪里调用了这个方法。他没有在方法名后面加上括号,所以编译器认为他试图从变量而不是方法中获取值。在 VB 和 Delphi 中,这些括号是可选的,但在 C# 中,它们是必需的。因此,要更正原始帖子的最后一行,Console.WriteLine("{0}",x.fullNameMethod());

