在 C# 中,变量是我们赋予内存位置的名称,每个变量都有一个指定的类型,用于指定可以存储在变量中的值的类型。所有变量应在使用前声明;每个变量都有一个特定的类型,决定变量的大小和范围。要对变量执行任何操作,必须定义具有特定数据类型的变量,以指定该变量在我们的应用程序中可以保存的数据类型。让我们看看关于变量的一些基本知识,
声明 C# 变量有一些规则:
C# 中变量定义的语法
<data_type> <variable_name>; <data_type> <variable_name>=value; <access_specifier><data_type> <variable_name>=value;
这里的
int name; float value; char _firstname;
您还可以在定义时初始化变量,如下所示,
int value = 100;
给变量赋值称为初始化,变量可以通过常量表达式用等号进行初始化,也可以在变量声明时进行初始化。
语法:
<data_type> <variable_name> = value;
或者
variable_name = value;
例如,
int value1=5, value2= 7; double pi= 3.1416; char name='Rock';
变量有多种类型,例如
在方法、块或构造函数中定义的局部变量。一旦声明了变量,这些变量就只存在于块内,我们只能在块内访问这些变量。该变量在调用函数或进入块时创建,并且在从块中存在后或从函数返回时将被销毁一次。
在示例程序中,变量“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(); } }
输出:
实例变量称为非静态变量;实例变量在类中声明,但在任何方法、块或构造函数之外声明。一旦类的对象创建,这些变量就会创建,当对象被销毁时,这些变量也会被销毁。对于实例变量,我们可以使用访问说明符。
程序中,实例变量为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); } }
输出:
静态变量在程序执行开始时创建并在执行结束时销毁。静态变量也称为类变量。为了访问静态变量,我们不需要创建类的对象;我们可以简单地访问变量,
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:
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:
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:
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中文网其他相关文章!