C#內置引用類型:object String Dynamic函數
C#徹底面向對象,沒有所謂的全局變量,取消了指針的使用,實例聲明用new,拋棄了多繼承,擁有自動內存管理編碼
數據輸出轉換相關:spa
using System; namespace test1 { class test1{ static void Main(String[] args) { //顯示強制轉換 double a = 22.23; int i = (int)a; Console.WriteLine(i); //C#內置類型轉換方法 float f = 0.12f; Console.WriteLine(f.ToString()); //object,類型檢測在編譯時發生 object o; o = 100; //裝箱 Console.WriteLine("o = {0}",o); int b = (int)o; //拆箱 Console.WriteLine("b = {0}",b); //dynamic,類型檢測在運行時發生 dynamic c = 99,d = 1.23; Console.WriteLine("c = {0},d = {1}",c,d); //字符串String String str1 = "gbb"; String str2 = @"/ybb"; //字符串前面加@,裏面的特殊字符當作普通字符輸出 String str3 = "a b" + //字符串裏能夠任意換行、空格 "c"; Console.WriteLine(str1); Console.WriteLine(str2); Console.WriteLine(str3); Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { String str1 = "gbb" + Environment.NewLine + "ybb"; //字符串中有換行回車的正確方式 Console.WriteLine("str1:\n{0}",str1); String str2 = "gbb\r\nybb"; //字符串中硬編碼了回車符和換行符,通常不建議,這樣有可能不支持跨平臺 Console.WriteLine("str2:\n{0}",str2); //字符串能夠使用「+」鏈接 String str3 = "abc" + " " + "def"; Console.WriteLine("str3:{0}",str3); Console.ReadKey(); } } }
整數常量的前綴、後綴:均不區分大小寫指針
0x 十六進制 0 八進制 無前綴 十進制code
後綴:對象
U unsigned L long繼承
運算符描述實例 :內存
sizeof()返回數據類型的大小。sizeof(int),將返回 4. ci
typeof()返回 class 的類型。typeof(StreamReader); 字符串
&返回變量的地址。&a; 將獲得變量的實際地址。
*變量的指針。*a; 將指向一個變量。
? :條件表達式 若是條件爲真 ? 則爲 X : 不然爲 Y is判斷對象是否爲某一類型。
If( Ford is Car) // 檢查 Ford 是不是 Car 類的一個對象。
as強制轉換,即便轉換失敗也不會拋出異常。
Object obj = new StringReader("Hello");
StringReader r = obj as StringReader;
using System; namespace test1 { class test1{ static void Main(String[] args) { Console.WriteLine("input a inteager:"); int num; num = Convert.ToInt32(Console.ReadLine()); //readLine()只接受字符串 Console.WriteLine("the number input:{0}",num); //進制轉換 decimal d = (decimal)0x20; Console.WriteLine("轉換爲十進制爲:{0}",d); Console.ReadKey(); } } }
訪問修飾符進行C#的封裝:
public 任何公有成員能夠被外部類訪問
private 同一個類中的函數成員能夠訪問
protected 子類訪問基類的成員函數成員變量,有助於實現繼承
internal 可被同一應用程序中的任何類或方法訪問
protected internal 容許一個類將其成員變量和成員函數對同一應用程序內的子類之外的其餘的類對象和函數進行隱藏
引用 ref
using System; namespace test1 { class test1{ public int num = 50; //不加public則不能被其餘類調用 public int getMax(ref int a,ref int b) //傳引用,public做用同上 //protected internal int getMax(ref int a,ref int b) //用protected internal也能夠,不太明白,作個標記 { if(a > b) { return a; } else { return b; } } } class test2{ public static void Main(String [] args) { test1 t = new test1(); int x = 20,y = 30; Console.WriteLine("The Max:{0},the number:{1}",t.getMax(ref x,ref y),t.num); Console.ReadLine(); } } }
using System; namespace test1 { class test1 { public static void Main(String [] args) { //可空類型 ? bool ?val = new bool?(); int ? num1 = null; int ? num2 = 20; double ? num3 = null; float ? num4 = 1.23f; double ? num5 = new double?(); Console.WriteLine("{0},{1},{2},{3},{4},{5}",val,num1,num2,num3,num4,num5); //null的合併運算 ?? int num; num = num1 ?? 9; Console.WriteLine("num:{0}",num);//第一個數爲空,返回第二個數的值 num = num2 ?? 9; Console.WriteLine("num:{0}",num);//第一個數非空,返回第一個數的值 Console.ReadLine(); } } }