C#相关基础知识点总结+基础代码
生活随笔
收集整理的這篇文章主要介紹了
C#相关基础知识点总结+基础代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#基礎知識
同一命名空間下的兩個類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace HelloWorld {class A{private int a;public A(int x) { a = x; }public void show() { Console.WriteLine(a); }}class Program{static void Main(string[] args){A a = new A(100);a.show();Console.ReadKey();}} }基礎數據類型
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace HelloWorld { class Program{static void Main(string[] args){sbyte sbt = 127; //8位有符號數,128超出范圍,強制轉換都不行Console.WriteLine(sbt);short sht = 10000; //16位有符號數int a = 1000000; //32位有符號數long b = 100000000000; //64位有符號數//無符號整型有: byte ushort uint ulongchar c = 'A'; //字符型,而且無法隱式轉換,可以顯示轉換Console.WriteLine(c);//浮點型有: float(32位) double(64位) decimal(128位)decimal dem = 1e-9M; //不能隱式轉換,一般的數默認double, 加后綴M或者強制轉換Console.WriteLine(dem);bool flag = true; //bool型Console.WriteLine(flag);Console.ReadKey();}} }結構體
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace HelloWorld { struct A{int a,b;public A(int a=0,int b = 0)//跟C語言不同,有訪問級別{this.a = a;this.b = b;}public void show() { Console.WriteLine(a*b); }}class Program{static void Main(string[] args){A obj =new A(12,13);obj.show();Console.ReadKey();}} }string字符串
string s = "hello world"; Console.WriteLine(s);強制轉換和格式
string s = Console.ReadLine(); double a = double.Parse(s); //強制轉換 Console.WriteLine(a*1.7); //默認最多保留14位 s = String.Format("半徑={0:f2}\n周長={1:f2}", a, 2 * 3.14* a); Console.WriteLine(s);空類型值
int? a; //表明一個值可以為空類型 a = null; int b; if (a.HasValue)b = (int)a; //不能隱式轉換 elseb = 1; Console.WriteLine(b);字符型
char ch = 'A'; char ch = '\101'; // 用8進制數表示ASCII字符,最多3位 char ch = '\x41'; //用2位16進制數表示ASCII字符 char ch = '\x0041'; //用低2位16進制數表示ASCII字符 char ch ='\u0041'; //Unicode字符,必須4位16進制數char數據常用方法
char c =(char) Console.Read(); Console.WriteLine(c); if (char.IsDigit(c))Console.WriteLine(c);//char.方法 的格式string用法以及比較
char[] ch = { 'a', 'a', 'a' }; string s1 = new string(ch); string s2 = "aa" + "a"; Console.WriteLine(s1==s2); Console.WriteLine(s1.Equals(s2)); Console.WriteLine(s1.CompareTo(s2));@的用法
C#字符串可以@開頭,并用雙引號引起來:√ string s3 = @"c:\myFolder\myFile.txt"; 若要在一個用 @ 引起來的字符串中包括一個雙引號,則應使用兩個雙引號:例如: "You!" cried the captain.則用: @"""You!"" cried the captain."常見數值輸出格式
C 或 c 貨幣 Console.Write("{0:C}", 2.5); //$2.50 Console.Write("{0:C}", -2.5); //($2.50)D 或 d 十進制數 Console.Write("{0:D5}", 25); //00025E 或 e 科學型 Console.Write("{0:E}", 250000); //2.500000E+005F 或 f 固定點 Console.Write("{0:F2}", 25); //25.00 Console.Write("{0:F0}", 25); //25G 或 g 常規 Console.Write("{0:G}", 2.5); //2.5N 或 n 數字 Console.Write("{0:N}", 2500000); //2,500,000.00X 或 x 十六進制 Console.Write("{0:X}", 250); //FA Console.Write("{0:X}", 0xffff); //FFFF類型轉換
string s = "123"; int a = System.Convert.ToInt32(s); Console.WriteLine(a); s = "2016/02/27"; DateTime dt = System.Convert.ToDateTime(s); Console.WriteLine(dt);日期類型
DateTime date1 = DateTime.Now; //獲得當前系統日期和時間 DateTime date2 = new DateTime(2014,10,1); //年月日 Console.WriteLine(date1); //日期和時間 Console.WriteLine(date2.ToLongDateString()); //長日期 2014年10月1日 Console.WriteLine(date2.ToShortDateString()); //短日期 2014/10/1 Console.WriteLine(date1.Year); //年 Console.WriteLine(date1.Hour); //時 Console.WriteLine(date2.ToString("yyyy-MM-dd")); //格式化 2014-10-01object類型
裝箱:將值類型轉化為object類型int i=10;object o1=i; //隱式裝箱object o2=(object)i; //顯式裝箱拆箱:將obj類型轉化為一個值類型int i=10;object obj=i; //裝箱int j=(int)obj; //拆箱var弱類型
1. var變量必須在定義時初始化:var i=100; //OKvar s; s = "abcd"; //NO,必須var s= "abcd"; 2. 不能給var變量賦與初始化值類型不同的值:var s= "abcd"; s="hello"; //OKs=100; //NO 3. var要求是局部變量。 4. var定義變量和object不同,它在效率上和使用強類型方式定義變量完全一樣。(裝箱與拆箱的過程是很損耗性能的)ref引用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace HelloWorld { class Program{static void Swap(ref int a, ref int b){int t=a;a = b;b = t;}static void Main(string[] args){int a = 3, b = 4;Console.Write("{0},{1}\n",a,b);Swap(ref a,ref b); //要加refConsole.Write("{0},{1}\n",a,b);Console.ReadKey();}} }foreach用法
int odd = 0, even = 0; int[] arr = { 1, 3, 5, 8, 11 }; foreach(int x in arr)if (x % 2 == 0)even++;elseodd++; Console.WriteLine("{0},{1}",odd,even);異常處理
try { //執行的代碼(可能有異常)。//一旦發現異常,則立即跳到catch執行。 } catch { //處理異常。(若try行代碼沒有異常,則不執行catch內代碼) } finally { //不管是否有異常都會執行finally,包括catch 里面用了return。 }二維數組和交錯數組
int[,] a = new int[2, 3]; for (int i = 0; i < 2; i++)for (int j = 0; j < 3; j++)a[i,j] = i * 3 + j; for(int i=0; i<2; i++) {for (int j = 0; j < 3; j++)Console.Write("{0} ",a[i,j]);Console.WriteLine(); } /* 交錯數組 */ int[][] jaggedArr = new int[4][]; //變長 jaggedArr[0] = new int[] { 1, 3, 5, 7, 9, 11 }; jaggedArr[1] = new int[] { 1, 1 }; jaggedArr[2] = new int[] { 2, 4, 6 }; jaggedArr[3] = new int[] { 1, 0, 0, 0, 1 };ArrayList
ArrayList arrlist1 = new ArrayList(); //新增數據,可以是任何類型 arrlist1.Add(123); arrlist1.Add("abc"); //修改數據 arrlist1[1] = 100; //插入數據 arrlist1.Insert(1, "xyz"); //移除數據 //arrlist1.RemoveAt(0); Console.WriteLine("元素個數=" + arrlist1.Count ); foreach (var x in arrlist1) Console.WriteLine(x);List<string> list = new List<string>(); list.Add("Tom"); list.Add("Mary"); list.Insert(1, "Mike"); //添加數組 string[ ] ss = { "Jerry", "Jim", "David" }; list.AddRange(ss); list.Remove("Jerry"); list.RemoveAt(0); //刪除 list.Sort(); //排序,默認升序 list.Reverse(); //反序 if( ! list.Contains("Jerry"))Console.WriteLine("Jerry已經不存在了!"); Console.WriteLine("元素個數=" + list.Count); foreach (string x in list) Console.WriteLine(x); list.Clear(); //清空總結
以上是生活随笔為你收集整理的C#相关基础知识点总结+基础代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vob是什么文件
- 下一篇: 音响测量时一定要关掉声卡的这一功能音响测