C#返回一个字符串方法

use*_*498 -1 c# string methods return class

我试图从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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ell 11

您当前正在尝试访问类似于属性的方法

Console.WriteLine("{0}",x.fullNameMethod);
Run Code Online (Sandbox Code Playgroud)

应该

Console.WriteLine("{0}",x.fullNameMethod());
Run Code Online (Sandbox Code Playgroud)

或者你可以把它变成一个proprty使用

public string fullName
{
   get
   {
        string x = firstName + " " + lastName;
        return x;
   }
}
Run Code Online (Sandbox Code Playgroud)