关键字的含义 [英] Meaning of a keyword

查看:113
本文介绍了关键字的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在理解Void关键字的含义时,我需要帮助.

Please I need help in understanding what the keyword Void means.

我知道这意味着它不返回值.但是我仍然对表达式返回值"有疑问.

I know it means that it does not return value. But I still have problem with that expression "Return value".

请指的是什么值?

因为我认为这样的代码正在返回值:

because I think that a code like this is returning value:

班级编号

{

p ublic无效main()

  {

int x;

int y;

x = 2;

y = 2;

Console.WriteLine(x + y);

Console.WriteLine(x+y);

}

}

现在的答案是:4

我的问题是,4不是返回的值吗?

推荐答案

在C#中,返回值是从函数(方法)返回的值.在您提供的示例中,"x + y"表示是一个 expression ,在执行计算后,确实的值为4.  但是返回值在C#中具有特定含义,而表达式则不会 返回一个值.  请考虑以下示例:

In C#, a return value is a value that is returned from a function(method).  In the example you give, "x+y" is an expression, that indeed does have a value of four after performing the calculation.  But returning a value has specific meaning in C# and expressions don't return a value.  Consider this example:

class  number
{ 
    public static void Main() 
    {
        int x = 2;
        int y = 2;
        int result = Add(x, y);
        Console.WriteLine(result);
    }

    public static int Add(int num1, int num2) 
    {
        return num1 + num2;
    }
} 

在此示例中,Add是一个返回值的函数.在这种情况下,我将x和y传递给Add,将它们分配给参数num1和num2.我将num1和num2相加并返回该表达式的结果.

In this example, Add is a function that returns a value.  In this case, I pass x and y to Add where they are assigned to the parameters num1 and num2.  I add num1 and num2 and return the result of that expression.

在这种情况下,返回任何内容的唯一函数是Add.  正在运行的其他两个函数是Main和Console.WriteLine,它们都是void类型,因此不会返回任何内容.

In this case, the only function that returned anything is Add.  The other two functions in play are Main and Console.WriteLine which are both of type void and so don't return anything.

请注意,在C#中,Main函数实际上返回int值是常见的,但不是必需的.  该值返回给操作系统,并且可以由启动您的程序的程序(或命令行)查询.  按照惯例,如果主 函数返回零,则认为您的程序已成功运行,并且使用非零值表示错误.  (如果Main函数的类型为void,则它在完成后将向操作系统返回零).

Note that in C# it is common, but not required, for the Main function to actually return an int value.  This value is returned to the OS and can be queried by the program (or command line) which launched your program.   By convention if the Main function returns zero your program is considered to have run successfully and a non-zero value is used to indicate an error.  (If the Main function is of type void it will return zero to the OS upon completion).


这篇关于关键字的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆