型別和變數
[C#型別和變數(原文參考官方教程)]https://docs.microsoft.com/zh-cn/dotnet/csharp/tour-of-csharp/types-and-variables
C#有兩種型別:
1. 值型別
1. 簡單型別
- 有符號的整型:sbyte、short、int、long
- 無符號的整型:byte、ushort、uint、ulong
- Unicode 字符:char
- IEEE 二進制浮點:float、double
- 高精度十進制浮點數:decimal
- 布爾:bool
2. 列舉型別
格式為 enum E {...} 的用戶定義型別
每個列舉型別都有一個可以為任意整型數值型別的基礎型別
與C++相同,列舉數可以使用初始值設定項來替代默認值,默認從0開始的整型數值
public class EnumTest
{
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
static void Main()
{
int x = (int)Day.Sun;
int y = (int)Day.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
}
}
/* Output:
Sun = 0
Fri = 5
*/
3. 結構型別
格式為 struct S {...} 的用戶定義型別
struct是一種值型別,只要是用于封裝小型資料
例如把“書”這個物體所包含的價格,書名和作者三個資料封裝成結構體Book
public struct Book
{
public decimal price;
public string title;
public string author;
}
4. 可為null的值型別
值為 null 的其他所有值型別的擴展
在標準型別下的擴展,是 System.Nullable<T> 結構的實體
以int型別作為測驗,int型別本身不能初始化為null型別
int? x = null;
if (x.HasValue)
{
Console.WriteLine($"x is {x.Value}");
}
else
{
Console.WriteLine("x does not have a value");
}
輸出:
x does not have a value
2. 參考型別
1. 型別別
- 其他所有型別的最侄訓類:objectUnicode
- 字串:string
- 格式為 class C {...} 的用戶定義型別
C#中,一個類只能從一個基類繼承實作,但是一個類可以實作多個介面,
| 繼承 | 示例 |
|---|---|
| 無 | class ClassA { } |
| single | class DerivedClass: BaseClass { } |
| 無,實作兩個介面 | class ImplClass: IFace1, IFace2 { } |
| 無,實作一個介面 | class ImplDerivedClass: BaseClass, IFace1 { } |
類成員(包括嵌套的類)可以是 public、protected internal、protected、internal、private 或 private protected, 默認情況下成員為 private,
public:同一程式集中的任何其他代碼或參考該程式集的其他程式集都可以訪問該型別或成員,
private:只有同一類或結構中的代碼可以訪問該型別或成員,
protected:只有同一類或者從該類派生的類中的代碼可以訪問該型別或成員,
internal:同一程式集中的任何代碼都可以訪問該型別或成員,但其他程式集中的代碼不可以,
protected internal:該型別或成員可由對其進行宣告的程式集或另一程式集中的派生類中的任何代碼訪問,
private protected:只有在其宣告程式集內,通過相同類中的代碼或派生自該類的型別,才能訪問型別或成員,
注:
被編譯到同一個dll或exe中的程式就是處于同一個程式集中,在不同的dll或exe檔案中的程式就是處于不同的程式集中,.net中的程式集就是一個編譯器直接生成的dll或可執行的exe檔案,包含程式集清單、元資料和MSIL等,是一個或者多個型別定義及資源檔案的集合體,
下面的示例說明如何宣告類欄位、建構式和方法, 該示例還說明如何實體化物件及如何列印實體資料, 本例宣告了兩個類, 第一個類 Child 包含兩個私有欄位(name 和 age)、兩個公共建構式和一個公共方法, 第二個類 StringTest 用于包含 Main,
class Child
{
private int age;
private string name;
// 無引數建構式
public Child()
{
name = "N/A";
}
// 帶引數的建構式
public Child(string name, int age)
{
this.name = name;
this.age = age;
}
// Printing method:
public void PrintChild()
{
Console.WriteLine("{0}, {1} years old.", name, age);
}
}
class StringTest
{
static void Main()
{
// Create objects by using the new operator:
Child child1 = new Child("Craig", 11);
Child child2 = new Child("Sally", 10);
// Create an object using the default constructor:
Child child3 = new Child();
// Display results:
Console.Write("Child #1: ");
child1.PrintChild();
Console.Write("Child #2: ");
child2.PrintChild();
Console.Write("Child #3: ");
child3.PrintChild();
}
}
/* Output:
Child #1: Craig, 11 years old.
Child #2: Sally, 10 years old.
Child #3: N/A, 0 years old.
*/
2. 介面型別
格式為 interface I {...} 的用戶定義型別
介面只包含方法、屬性、事件或索引器的簽名, 實作介面的類或結構必須實作介面定義中指定的介面成員,
實體:
interface ISampleInterface //只定義宣告
{
void SampleMethod();
}
class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation: (具體的方法實作在類中實作)
void ISampleInterface.SampleMethod()
{
// Method implementation.(方法代碼)
}
static void Main()
{
// Declare an interface instance.
ISampleInterface obj = new ImplementationClass();
// Call the member.
obj.SampleMethod();
}
}
3. 陣列型別
一維和多維,例如 int[] 和 int[,]
可以將同一型別的多個變數存盤在一個陣列資料結構中, 通過指定陣列的元素型別來宣告陣列,
class TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array.
int[] array1 = new int[5];
// Declare and set array element values.
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax.
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// Declare a two dimensional array.
int[,] multiDimensionalArray1 = new int[2, 3];
// Declare and set array element values.
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// Declare a jagged array.
int[][] jaggedArray = new int[6][];
// Set the values of the first array in the jagged array structure.
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
}
}
4. 委托型別
格式為 delegate int D(...) 的用戶定義型別
具體參考:事件與委托學習筆記03
https://www.cnblogs.com/asahiLikka/p/11644393.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/120874.html
標籤:C#
下一篇:String類的方法應用
