c# - 方法必须有返回类型 (C#)

标签 c# methods return

所以我一直在关注 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/

相关文章:

c# - 将 JObject 转换为动态对象

c# - 如何禁用 WPF MenuItem 中的助记符?

java - 缺少带开关的返回语句

c++ - 为什么以下代码会返回错误的计数值?

c - 这里的返回是如何进行的?

c# - 如何访问用AddSingleton<T>注入(inject)的后台服务的成员

c# - 如何从 Entity Framework 4.3 代码优先模型生成 DDL 脚本?

python - Python 类的最大方法数是多少?

java - 按值/引用传递,什么?

从另一个方法调用时,Javascript 单例私有(private)函数不会访问对象属性