Csharp 简明教程

C

变量不过是为我们程序可以操作的存储区域指定的名称。C# 中的每个变量都有一个特定的类型,它决定了该变量的内存大小和布局,该内存中可以存储的值范围,以及可以对该变量应用的操作集。

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable’s memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.

C# 中提供基本值类型可分为以下几大类:

The basic value types provided in C# can be categorized as −

Type

Example

Integral types

sbyte, byte, short, ushort, int, uint, long, ulong, and char

Floating point types

float and double

Decimal types

decimal

Boolean types

true or false values, as assigned

Nullable types

Nullable data types

C# 还允许定义其他变量值类型,如 enum ,以及变量引用类型,如 ,我们将在后续章节中予以介绍。

C# also allows defining other value types of variable such as enum and reference types of variables such as * class*, which we will cover in subsequent chapters.

Defining Variables

C# 中的变量定义语法如下:

Syntax for variable definition in C# is −

<data_type> <variable_list>;

此处,data_type 必须是有效的 C# 数据类型,包括 char、int、float、double 或任何用户定义的数据类型,而 variable_list 可能由一个或多个用逗号分隔的标识符名称组成。

Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable_list may consist of one or more identifier names separated by commas.

这里显示了一些有效的变量定义:

Some valid variable definitions are shown here −

int i, j, k;
char c, ch;
float f, salary;
double d;

您可以初始化变量,如下所示:

You can initialize a variable at the time of definition as −

int i = 100;

Initializing Variables

变量用等号后跟一个常量表达式进行初始化(赋值)。初始化的一般形式为:

Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is −

variable_name = value;

变量可以在其声明中初始化。初始化程序由等号后跟常量表达式组成,如下所示:

Variables can be initialized in their declaration. The initializer consists of an equal sign followed by a constant expression as −

<data_type> <variable_name> = value;

一些示例:

Some examples are −

int d = 3, f = 5;    /* initializing d and f. */
byte z = 22;         /* initializes z. */
double pi = 3.14159; /* declares an approximation of pi. */
char x = 'x';        /* the variable x has the value 'x'. */

最好对变量进行适当初始化,否则程序有时可能会产生意外结果。

It is a good programming practice to initialize variables properly, otherwise sometimes program may produce unexpected result.

以下示例使用各种类型的变量:

The following example uses various types of variables −

using System;

namespace VariableDefinition {
   class Program {
      static void Main(string[] args) {
         short a;
         int b ;
         double c;

         /* actual initialization */
         a = 10;
         b = 20;
         c = a + b;
         Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
         Console.ReadLine();
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

a = 10, b = 20, c = 30

Accepting Values from User

Console 类在 System 命名空间中提供了一个名为 ReadLine() 的函数,用于接受用户的输入并将其存储到一个变量中。

The Console class in the System namespace provides a function ReadLine() for accepting input from the user and store it into a variable.

例如,

For example,

int num;
num = Convert.ToInt32(Console.ReadLine());

函数 Convert.ToInt32() 将用户输入的数据转换为 int 数据类型,因为 Console.ReadLine() 接受的格式为字符串。

The function Convert.ToInt32() converts the data entered by the user to int data type, because Console.ReadLine() accepts the data in string format.

Lvalue and Rvalue Expressions in C

C# 中有两种表达式:

There are two kinds of expressions in C# −

  1. lvalue − An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.

  2. rvalue − An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

变量是 lvalue,因此它们可以出现在赋值运算的左边。数字文本是 rvalue,因此它们不能被赋值,不能出现在赋值运算的左边。以下是一个有效的 C# 语句:

Variables are lvalues and hence they may appear on the left-hand side of an assignment. Numeric literals are rvalues and hence they may not be assigned and can not appear on the left-hand side. Following is a valid C# statement −

int g = 20;

但是,以下不是有效的语句,将导致编译时错误:

But following is not a valid statement and would generate compile-time error −

10 = 20;