所以我一直在关注 C# 和这本书。
http://www.robmiles.com/c-yellow-book/Rob%20Miles%20CSharp%20Yellow%20Book%202011.pdf 在第 81-82 页上,我从那里得到这段代码,并从第 82 页添加另一个方法,导致:
using System;
enum AccountState
{
New,
Active,
UnderAudit,
Frozen,
Closed
};
struct Account
{
public AccountState State;
public string Name;
public string Address;
public int AccountNumber;
public int Balance;
public int Overdraft;
};
class Bankprogram
{
public static void Main()
{
Account RobsAccount;
RobsAccount.State = AccountState.Active;
RobsAccount.Name = "Rob Miles";
RobsAccount.AccountNumber = 1234;
RobsAccount.Address = "his home";
RobsAccount.Balance = 0;
RobsAccount.Overdraft = -1;
Console.WriteLine("name is " + RobsAccount.Name);
Console.WriteLine("balance is : " + RobsAccount.Balance );
}
public void PrintAccount(Account a)
{
Console.WriteLine ("Name" + a.Name);
Console.WriteLine ("Address :" + a.Address);
Console.WriteLine ("Balance:" + a.Balance);
}
PrintAccount(RobsAccount);
}
但我得到一个错误:方法必须有返回类型。引用“PrintAccount(RobAccount);”
我知道以前有人问过这个问题,但没有一个看起来与我的问题相似。
最佳答案
问题是编译器认为 PrintAccount(RobsAccount);
是一个方法定义,这就是为什么需要返回类型。
你必须在另一个方法中调用那个方法,你不能在 void 上调用它。
关于c# - 方法必须有返回类型 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11993964/