在 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中文網其他相關文章!