C#學習筆記6。本站提示廣大學習愛好者:(C#學習筆記6)文章只能為提供參考,不一定能成為您想要的結果。以下是C#學習筆記6正文
1.構造:構造除了可以含有屬性和字段,還可以包辦法和結構器,但不能包括黠認(無參數}的結構器。有的時分(比方在實例化一個數組的時分)不會調用值類型的結構器,由於一切數組內存都轉為用零來初始化,為了防止由於默許結構器只是偶然調用而形成不分歧,C#完全制止了用戶顯式定義默許結構器,由於編譯器會將聲明時的實例字段賦值放到類型的結構器中停止。在結構器中必需對 struct中的一切字段停止初始化,假如沒有做到這一點,就會發生編譯錯誤,可檢查Angle構造的代碼。
2.構造的承繼與接口:一切值類型都是密封的,除此之外,一切值類型都派生自system.ValueType,這意味著struct的承繼鏈息是從object到ValueType到struct。值類型還能完成接口,許多接口都是完成接口框架固定組成局部,比方 IComparilble 和IFormattable。
3.裝箱與拆箱:裝箱就是把值類型變成援用類型,如下:
(1) 首先在堆中分配好內存,它將用於寄存值類型的數據以及少許額定開支;
(2) 接著發作一次內存復制舉措,棧上的值類型數據復制到堆上分配好的地位;
(3) 最後,對象或接口援用失掉更新,指向堆上的地位;
拆箱就是把援用類型變成值類型,如下:依據定義,CIL 指令 unbox 只是對堆上的數據停止解援用,並不包括從堆復制到棧的舉措。但在 C#言語中,太少數時分緊接著在拆箱之後發作一次復制舉措。裝箱和拆箱之所以重要,是由於裝箱去對功能和行為形成一些影響。開發者可以經過檢查CIL,在一個特定的代碼片段中統計 box/unbox 指令的數量。在BoxAndUnbox()中的代碼就存在屢次的裝箱與拆箱,這樣編寫的代碼是不合理。
4.枚舉:枚舉和其他值類型稍有不同,由於枚舉的承繼鏈是從System.ValueType到System.Enum,再到enum。
5.枚舉與字符串的轉換:枚舉ToString()後會輸入枚舉標識符,運用Enum.Parse或Enum.TryParse辦法可以把字符串轉化為枚舉,後一個辦法是.Net4.0新增的泛型辦法。此中我們也可以運用Enum.IsDefined()辦法來反省一個值能否包括在一個枚舉中。
6.枚舉作為“位標志”運用:
(1)可以檢查如下“FileAttributes“枚舉的設定(即System.IO。FileAttributes的設定),作為位標志後,其值可以自在組合,所以可以運用Or運算符來聯合枚舉值。如本示例中BitFlag()辦法的運用。當然枚舉中的每個值不一定只對應一個標志,完全可以為常用的標志組合定義額定的枚舉值。
(2)運用位標志類型的時分,位標志枚舉應該包括[FlagsAttribute]這個特性,這個標志指出多個枚舉值可以組合運用,此外,它改動了ToString()和Parse()辦法的行為。例如為一個已用FlagsAttribute修飾了的枚舉調用ToString()辦法,會為已設置的每個枚舉標志輸入對應的字符串(如BitFlag2()的示例),而假如沒有這個修飾,前往的就是組合後數值。
public struct Angle
{
public Angle(int hours, int minutes, int seconds)
{
Hours = hours;
Minutes = minutes;
Seconds = seconds;
}
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public Angle Move(int hours, int minutes, int seconds)
{
return new Angle(Hours + hours, Minutes + minutes, Seconds + seconds);
}
}
[Flags]
public enum FileAttributes
{
ReadOnly = 1 << 0,
Hidden = 1 << 1,
System = 1 << 2,
Directory = 1 << 3,
Archive = 1 << 5,
Device = 1 << 6,
Normal = 1 << 7,
Temporary = 1 << 8,
SparseFile = 1 << 9,
ReparsePoint = 1 << 10,
Compressed = 1 << 11,
Offline = 1 << 12,
NotContentIndexed = 1 << 13,
Encrypted = 1 << 14
}
public void BitFlag()
{
string fileName = @"enumtest.txt";
FileInfo file = new FileInfo(fileName);
file.Attributes = FileAttributes.Hidden | FileAttributes.ReadOnly;
Console.WriteLine("{0} | {1} = {2}", FileAttributes.Hidden, FileAttributes.ReadOnly, (int)file.Attributes);
if ((file.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
{
throw new Exception("File is not hidden");
}
if ((file.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
{
throw new Exception("File is not read-only");
}
//....
}
public void BitFlag2()
{
string fileName = @"enumtest.txt";
FileInfo file = new FileInfo(fileName);
file.Open(FileMode.Create).Close();
FileAttributes startingAttributes = file.Attributes;
file.Attributes = FileAttributes.Hidden | FileAttributes.ReadOnly;
Console.WriteLine("\"{0}\" output as \"{1}\"", file.Attributes.ToString().Replace(",", "|"), file.Attributes);
FileAttributes attributes;
Enum.TryParse(file.Attributes.ToString(), out attributes);
Console.WriteLine(attributes);
File.SetAttributes(fileName, startingAttributes);
file.Delete();
}
public void BoxAndUnbox()
{
int totalCount;
ArrayList list = new ArrayList();
Console.Write("Enter a number between 2 to 1000:");
totalCount = int.Parse(Console.ReadLine());
list.Add((double)0);
list.Add((double)1);
for (int i = 2; i < totalCount; i++)
{
list.Add((double)list[i - 1] + (double)list[i - 2]);
}
foreach (double num in list)
{
Console.Write("{0},", num);
}
}
----------------------以上內容依據《C#實質論 第三版》停止整理