C# 中的变量

WBOY
发布: 2024-09-03 15:02:50
原创
884 人浏览过

在 C# 中,变量是我们赋予内存位置的名称,每个变量都有一个指定的类型,用于指定可以存储在变量中的值的类型。所有变量应在使用前声明;每个变量都有一个特定的类型,决定变量的大小和范围。要对变量执行任何操作,必须定义具有特定数据类型的变量,以指定该变量在我们的应用程序中可以保存的数据类型。让我们看看关于变量的一些基本知识,

  • 变量只不过是赋予数据值的名称。
  • 变量可以保存特定数据类型的值,例如 int、string、float 等。
  • 变量的声明和初始化是在单独的语句中。
  • 变量可以用逗号分隔多个来定义,也可以单行或多行定义,直到分号结束。
  • 在使用变量之前必须先将值赋给它;否则,它将显示编译时错误。
  • 变量的值可以随时更改,直到程序可访问。

如何在 C# 中声明变量?

声明 C# 变量有一些规则:

  • 我们必须定义一个由数字、字母和下划线组合的变量名。
  • 每个变量名称都应以字母或下划线开头。
  • 变量名称之间不应有任何空格。
  • 变量名称不应包含任何保留关键字,如 int、char、float 等。

C# 中变量定义的语法

<data_type> <variable_name>;
<data_type> <variable_name>=value;
<access_specifier><data_type> <variable_name>=value;
登录后复制

这里的是一种数据类型,其中变量可以保存的类型有整数、Sting、浮点等等 是在我们的应用程序中保存值的变量的名称, 为变量分配特定值,最后 用于授予变量的访问权限。它们是一些合适的方法来描述 C# 编程语言中的变量名称。

int name;
float value;
char _firstname;
登录后复制

您还可以在定义时初始化变量,如下所示,

int value = 100;
登录后复制

如何在 C# 中初始化变量?

给变量赋值称为初始化,变量可以通过常量表达式用等号进行初始化,也可以在变量声明时进行初始化。

语法:

<data_type> <variable_name> = value;
登录后复制

或者

variable_name = value;
登录后复制

例如,

int value1=5, value2= 7;
double pi= 3.1416;
char name='Rock';
登录后复制

C# 中的变量类型及示例

变量有多种类型,例如

  1. 局部变量
  2. 实例变量或非静态变量
  3. 静态变量或类变量
  4. 常量变量
  5. 只读变量

1.局部变量

在方法、块或构造函数中定义的局部变量。一旦声明了变量,这些变量就只存在于块内,我们只能在块内访问这些变量。该变量在调用函数或进入块时创建,并且在从块中存在后或从函数返回时将被销毁一次。

在示例程序中,变量“customer_age”是函数 GetAge() 的局部变量。一旦我们在 GetAge() 函数之外应用变量 customer_age,编译器就会生成错误。

示例程序 – 局部变量

using System;
class CustomerEntry
{
public void GetAge()
{
int customer_age=0;         // local variable
customer_age= customer_age+28;
Console. WriteLine("Customer Age: "+ customer_age);
}
public static void Main(String[] args)
{
CustomerEntry _customerObj=new CustomerEntry();
_customerObj.GetAge();
}
}
登录后复制

输出:

C# 中的变量

2.实例变量或非静态变量

实例变量称为非静态变量;实例变量在类中声明,但在任何方法、块或构造函数之外声明。一旦类的对象创建,这些变量就会创建,当对象被销毁时,这些变量也会被销毁。对于实例变量,我们可以使用访问说明符。

程序中,实例变量为markEnglish、markMaths。我们可以创建多个对象,每个对象都有其实例变量的副本。

示例程序 – 实例变量

using System;
class StudentMarks {
// instance variables
int markEnglish;
int markMaths;
int markPhysics;
public static void Main(String[] args) // Main Method
{
StudentMarks obj1 = new StudentMarks ();  //Object creation 1
obj1. markEnglish = 90;
obj1. markMaths = 80;
obj1. markPhysics = 93;
StudentMarks obj2 = new StudentMarks (); //Object creation 1
obj2. markEnglish = 95;
obj2. markMaths = 70;
obj2. markPhysics = 90;
Console.WriteLine("Marks Obtained from first object:");
Console.WriteLine(obj1. markEnglish);
Console.WriteLine(obj1. markMaths);
Console.WriteLine(obj1. markPhysics);
Console.WriteLine("Marks obtained from second object:");
Console.WriteLine(obj2. markEnglish);
Console.WriteLine(obj2. markMaths);
Console.WriteLine(obj2. markPhysics);
}
}
登录后复制

输出:

C# 中的变量

3.静态变量或类变量

静态变量在程序执行开始时创建并在执行结束时销毁。静态变量也称为类变量。为了访问静态变量,我们不需要创建类的对象;我们可以简单地访问变量,

Class_name.variable_name;
登录后复制

静态变量是在类内或任何方法或构造函数外使用关键字 static 声明的。

Sample Program – Static Variable

using System;
class Employee
{
static double empSalary;
static string empName="Smith";
public static void Main(String[] args)
{
Employee.empSalary=100000;  // accessing the static variable
Console. WriteLine(Employee.empName+ "'s Salary:" + Employee.empSalary);
}
}
登录后复制

Output:

C# 中的变量

4. Constants Variables

Constant variables are similar to the static variables, once initialized and the one-time life cycle of a class and it does not need the instance of the class for initializing or accessing. The constant variable is declared by using the ‘const’ keyword, these variables cannot be altered once it declared, and it should be initialized at the time of the declaration part only.

Sample Program – Constant Variable

using System;
class Program_A
{
int x= 25; // instance variable
static int y= 35; // static variable
const float maxValue =75; // constant variable
public static void Main()
{
Program_A classObject= new Program_A(); // object creation
Console.WriteLine("Value of x : " + classObject.x);
Console.WriteLine("Value of y : " + Program_A.y);
Console.WriteLine("Value of max " + Program_A. maxValue);
}
}
登录后复制

Output:

C# 中的变量

5. Read-Only Variables

A read-only variable is declared using the keyword ‘read-only‘ and those variables cannot be altered like constant variables. The constant variable is an unchanging value for the entire class whereas read-only is a permanent value for a specific instance of a class. There is no compulsion to initialize a read-only variable at the time declaration, it can be initialized under constructor. The default value set to the variable is 0.

Sample Program – Read-Only

using System;
class Program_B
{
const float maxValue =75; // constant variable
readonly int x; // read-only variable
public static void Main()
{
Program_B classObject= new Program_B(); // object creation
Console.WriteLine("Value of max: " + Program_B. maxValue);
Console.WriteLine("Value of x : " + classObject.x);
}
}
登录后复制

Output:

C# 中的变量

Conclusion

Finally, you have known about how variables allow you to store data in different ways. In this article, we learned about how to declare and initialize variables and how to make use of it. I hope this article would have helped you out with the working process of variables.

以上是C# 中的变量的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!