Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
公共客户(int id)给出错误:方法必须具有返回类型C#_C# - Fatal编程技术网

公共客户(int id)给出错误:方法必须具有返回类型C#

公共客户(int id)给出错误:方法必须具有返回类型C#,c#,C#,在我的代码底部,突出显示了“CustomerEmail”,并指出该方法必须具有返回类型。然后“return突出显示为”,因为CustomerEmail(int)返回void,return关键字后面不能跟一个对象表达式“我搞不懂CustomerEmail是如何返回void的?我缺少什么吗?” 公共类CustomerCollection { List customerList=新列表() 公共列表客户列表 { 获取{return customerList;} 设置{customerList=valu

在我的代码底部,突出显示了“CustomerEmail”,并指出该方法必须具有返回类型。然后“return突出显示为”,因为CustomerEmail(int)返回void,return关键字后面不能跟一个对象表达式“我搞不懂CustomerEmail是如何返回void的?我缺少什么吗?”

公共类CustomerCollection { List customerList=新列表()

公共列表客户列表
{
获取{return customerList;}
设置{customerList=value;}
}
public void RegisterCustomer(int-id,字符串第一,字符串最后)
{
客户c=新客户(id、第一个、最后一个);
客户列表。添加(c);
}
public void register客户(int-id、字符串优先、字符串最后、字符串电话、字符串电子邮件)
{
客户c=新客户(id、第一个、最后一个、电话、电子邮件);
客户列表。添加(c);
}
public void RemoveCustomer(内部id)
{
//如果类中有一个单参数构造函数和Equals方法,则该方法有效
客户rem=新客户(id);
客户列表。删除(rem);
}
公共客户邮件(int id)
{
客户findEmail=新客户(id);
for(int i=0;i
对于不返回值的方法,必须声明一个返回类型(或
void
)。在这种情况下,
string
似乎是合适的。使用
Get
作为返回值的方法的前缀也是一种常见的约定:

  public string GetCustomerEmail(int id)
  {
      Customer findEmail = new Customer(id);
      for (int i=0; i < customerList.Count;i++)
          if (customerList[i].Equals(findEmail))
              return customerList[i].CustomerEmail;
      return null;
  }

您没有该方法的返回类型。因此,您的方法签名不正确

试着把它改成

public string CustomerEmail(int id)

但是,如果不希望返回任何内容,则需要使用
void
返回类型

查看MSDN文档,了解如何创建方法

方法可以向调用方返回值。如果返回类型(在方法名称之前列出的类型)不是无效的,则该方法可以使用return关键字返回值。带有return关键字,后跟与返回类型匹配的值的语句将向方法调用方返回该值。return关键字还将停止执行方法的终止。如果返回类型为void,则不带值的return语句仍有助于停止方法的执行。如果不带return关键字,方法将在到达代码块末尾时停止执行。具有非void返回类型的方法需要使用return关键字来返回值

将方法更改为

  public <return type> CustomerEmail(int id)
  {
      Customer findEmail = new Customer(id);
      for (int i=0; i < customerList.Count;i++)
          if (customerList[i].Equals(findEmail))
              return customerList[i].CustomerEmail;
      return null;
  }
publiccustomemail(int-id)
{
客户findEmail=新客户(id);
for(int i=0;i

这里的
返回类型
CustomerEmail的类型
。同样的解决方案适用于
FindCustomer
方法。

试试“公共字符串CustomerEmail”,这很有意义。谢谢你帮我解决了这个问题。我有一段时间走到了死胡同。
public string CustomerEmail(int id)
public Customer FindCustomer(int id)
  public <return type> CustomerEmail(int id)
  {
      Customer findEmail = new Customer(id);
      for (int i=0; i < customerList.Count;i++)
          if (customerList[i].Equals(findEmail))
              return customerList[i].CustomerEmail;
      return null;
  }