C#常用知識點簡單回顧(有圖有真相)

來源:互聯網
上載者:User

1)傳值調用與引用調用 複製代碼 代碼如下:using System;
class MethodCall
{
public static void Main()
{
/*
* 參數類型分為 in, ref, out 三種,預設為 in。
* in 類型在子方法中修改了對應變數後,主方法中的值不會發生改變。
* ref 類型在子方法中修改了對應變數後,主方法中的值也會發生改變。
* out 主方法中對應的變數不需要初始化。
*
*/
int a = 1, b = 2, c;
Console.WriteLine("Before Method Call : a = {0}, b = {1}, c 未賦值", a, b);
AMethod(a, ref b, out c);
Console.WriteLine("After Method Call : a = {0}, b = {1}, c = {2}", a, b, c);
Console.Read();
}
public static void AMethod(int x, ref int y, out int z)
{
x = 110;
y = 120;
z = 119;
}
}

2)列印三角形

複製代碼 代碼如下:using System;
public class Hello
{
public static void Main()
{
Console.Write("請輸入行數:");
int lines = int.Parse(Console.ReadLine());
Console.WriteLine("");
for(int i=1; i<=lines ; i++)
{
for(int k=1; k<= lines-i; k++)
Console.Write(" ");
for(int j=1; j<=i*2+1; j++)
Console.Write("*");
Console.WriteLine("");
}
Console.ReadLine();
}
}

3)遞迴求階乘

複製代碼 代碼如下:using System;
class Factor
{
public static void Main()
{
for(int i=1; i<=10; i++)
Console.WriteLine("{0} 的階乘是 {1}",i, Factorial(i));
Console.Read();
}
public static long Factorial(long n)
{
if(n == 1)
return 1;
else
return n * Factorial(n-1);
}
}

4)多態性

複製代碼 代碼如下:using System;
class Car
{
public virtual void Drive()
{ Console.WriteLine("Drive Car"); }
}
class Truck : Car
{
public override void Drive()
{ Console.WriteLine("Drive Truck"); }
}
class Client
{
public static void Main()
{
Car c = new Truck();
c.Drive(); //多態性決定著將調用Truck的Drive方法
Console.Read();
}
}

5)方法重載

複製代碼 代碼如下:using System;
class Client
{
public static void Main()
{
//重載是指方法名相同,方法的簽名不同
Console.WriteLine(Add(100,50));
Console.WriteLine(Add("100","50"));
Console.Read();
}
public static string Add(string a, string b)
{
return a + " add " + b;
}
public static int Add(int a, int b)
{
return a+b;
}
}

6)建構函式

複製代碼 代碼如下:using System;
public class Person
{
public string name = "";
public int age = 0;
//預設建構函式
public Person()
{
}
//建構函式重載(1)
public Person(int Age)
{
this.age = Age;
}
//建構函式重載(2)
public Person(int Age, string Name)
{
this.age = Age;
this.name = Name;
}
public void ShowInfo()
{
Console.WriteLine("姓名:" + name);
Console.WriteLine("年齡:" + age);
}
}
class Client
{
public static void Main()
{
Person p1 = new Person();
p1.ShowInfo();
Console.WriteLine("*************************");
Person p2 = new Person(25);
p2.ShowInfo();
Console.WriteLine("*************************");
Person p3 = new Person(25, "愛智旮旯");
p3.ShowInfo();
Console.Read();
}
}

7)靜態與非靜態

複製代碼 代碼如下:using System;
class StaticHello
{
public static void SayHello()
{ Console.WriteLine("Static Hello"); }
}
class NonStaticHello
{
public void SayHello()
{ Console.WriteLine("Non Static Hello"); }
}
class Client
{
public static void Main()
{
//靜態方法調用應當使用 “類名.方法”
StaticHello.SayHello();
//非靜態方法調用應當使用 “執行個體名稱.方法”
NonStaticHello h = new NonStaticHello();
h.SayHello();
Console.Read();
}
}

8)九九表

複製代碼 代碼如下:using System;
public class JiuJiuBiao
{
public static void Main(string[] args)
{
int i,j;
for(i=1; i<10; i++)
{
for(j=1; j<10; j++)
{
Console.Write("{0:D1}*{1:D1}={2,2} ", i, j, i*j);
}
Console.WriteLine("");
}
Console.ReadLine();
}
}

9)冒泡法排序

複製代碼 代碼如下:using System;
class ArraySort
{
public static void Main()
{
int[] d = {10,15,21,43,17,98,2,74,63,10};
int temp;
//冒泡法排序
for(int i=0; i<d.Length; i++)
for(int j=i+1; j<d.Length; j++)
if(d[i]<d[j])
{
temp = d[i];
d[i]=d[j];
d[j]=temp;
}
//輸出排序結果
foreach(int i in d)
Console.Write("{0}, ", i);
Console.Read();
}
}

10)求質數

複製代碼 代碼如下:using System;
class Factor
{
public static void Main()
{
for(int i=1; i<=100; i++)
if(IsPrime(i))
Console.WriteLine(i);
Console.Read();
}
public static bool IsPrime(int n)
{
for(int i=2; i<=Math.Sqrt(n); i++)
if(n%i == 0)
return false;
return true;
}
}

11)使用介面排序(1)

複製代碼 代碼如下:using System;
using System.Collections;
public class Person : IComparable
{
public int ID;
public string Rank;
public Person(int id, string rank)
{ this.ID=id; this.Rank = rank; }
#region IComparable Members
/*
* IComparable 介面只有一個方法: CompareTo。CompareTo方法
* 只接收一個object類型的參數,這意味著它可以接收任何類
* 型的資料(object是所有類的父類),這個方法會返回一
* 整型數值,含義如下:
*
* 1) 小於零,當前執行個體(this)小於obj對象
* 2) 等於零,當前執行個體(this)等於obj對象
* 3) 大於零,當前執行個體(this)大於obj對象
*
* Int32,Int16...,String,Decimal等資料類型都已經實現了IComparable介面
*/
public int CompareTo(object obj)
{
Person p = (Person)obj;
return this.ID.CompareTo(p.ID);
}
#endregion
}
class SortArrayList
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person(6, "排長"));
list.Add(new Person(3, "團長"));
list.Add(new Person(4, "司令"));
list.Add(new Person(5, "旅長"));
list.Add(new Person(7, "連長"));
list.Add(new Person(1, "軍長"));
list.Add(new Person(2, "營長"));
list.Add(new Person(8, "師長"));
list.Sort();
Console.WriteLine("After Sorting");
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
}
}

12)使用介面排序(2)

複製代碼 代碼如下:using System;
using System.Collections;
public enum enuSortOrder
{IDAsc, IDDesc, RankAsc, RankDesc}
public class Person : IComparable
{
public static enuSortOrder intSortOrder = enuSortOrder.IDAsc;
public int ID;
public string Rank;
public Person(int id, string rank)
{ this.ID=id; this.Rank = rank; }
#region IComparable Members
/*
* IComparable 介面只有一個方法: CompareTo。CompareTo方法
* 只接收一個object類型的參數,這意味著它可以接收任何類
* 型的資料(object是所有類的父類),這個方法會返回一
* 整型數值,含義如下:
*
* 1) 小於零,當前執行個體(this)小於obj對象
* 2) 等於零,當前執行個體(this)等於obj對象
* 3) 大於零,當前執行個體(this)大於obj對象
*
* Int32,Int16...,String,Decimal等資料類型都已經實現了IComparable介面
*/
public int CompareTo(object obj)
{
Person p = (Person)obj;
switch ((int)intSortOrder)
{
case (int)enuSortOrder.IDAsc:
return this.ID.CompareTo(p.ID);
case (int)enuSortOrder.IDDesc:
return p.ID.CompareTo(this.ID);
case (int)enuSortOrder.RankAsc:
return RankCompare(this.Rank, p.Rank);
case (int)enuSortOrder.RankDesc:
return RankCompare(p.Rank, this.Rank);
default:
return this.ID.CompareTo(p.ID);
}
}
private int RankCompare(string rank1, string rank2)
{
int intRank1 = ConvertRankToInt(rank1);
int intRank2 = ConvertRankToInt(rank2);
if(intRank1 < intRank2)
return -1;
else if(intRank1 == intRank2)
return 0;
else
return 1;
}
private int ConvertRankToInt(string rank)
{
if(rank == "司令")
return 8;
else if(rank == "軍長")
return 7;
else if(rank == "師長")
return 6;
else if(rank == "旅長")
return 5;
else if(rank == "團長")
return 4;
else if(rank == "營長")
return 3;
else if(rank == "連長")
return 2;
else
return 1;
}
#endregion
}
class SortArrayList
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person(6, "排長"));
list.Add(new Person(3, "團長"));
list.Add(new Person(4, "司令"));
list.Add(new Person(5, "旅長"));
list.Add(new Person(7, "連長"));
list.Add(new Person(1, "軍長"));
list.Add(new Person(2, "營長"));
list.Add(new Person(8, "師長"));
list.Sort();
Console.WriteLine("Sort By ID Asc:");
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
Console.WriteLine("----------------------------");
Console.WriteLine("Sort By ID Desc:");
Person.intSortOrder = enuSortOrder.IDDesc;
list.Sort();
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
Console.WriteLine("----------------------------");
Console.WriteLine("Sort By Rank Asc:");
Person.intSortOrder = enuSortOrder.RankAsc;
list.Sort();
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
Console.WriteLine("----------------------------");
Console.WriteLine("Sort By Rank Desc:");
Person.intSortOrder = enuSortOrder.RankDesc;
list.Sort();
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
}
}

13)屬性、方法作用範圍

複製代碼 代碼如下:using System;
class Base
{
/*
* public 的可存取範圍是所有類
* private 的可存取範圍是當前類
* protected 的可存取範圍是當前類及其子類
*/
public string name = "Tom";
private double salary = 1500;
protected int age = 20;
public virtual void ShowInfo()
{
Console.WriteLine(this.name); //可以,因為name是 public 型的
Console.WriteLine(this.salary); //可以,salary是private型,在Base類中可以訪問
Console.WriteLine(this.age); //可以,因為age是protected型,在子類中可以訪問
}
}
class Derived : Base
{
public override void ShowInfo()
{
Console.WriteLine(this.name); //可以,因為name是 public 型的
//Console.WriteLine(this.salary); //不可以,salary是private型,超出Base就無法訪問
Console.WriteLine(this.age); //可以,因為age是protected型,在子類中可以訪問
}
}
class Client
{
public static void Main()
{
Base b = new Base();
Console.WriteLine(b.name); //可以,因為name是 public 型的
//Console.WriteLine(this.salary); //不可以,salary是private型,超出Base就無法訪問
//Console.WriteLine(this.age); //不可以,因為age是protected型,Client不是Base的子類
Console.WriteLine("==========================");
b.ShowInfo();
Console.WriteLine("==========================");
Derived d = new Derived();
d.ShowInfo();
}
}

15)欄位與屬性

複製代碼 代碼如下:using System;
class SumToHundred
{
public static void Main()
{
int sum=0;
for(int i=1; i<=100; i++)
sum += i;
Console.WriteLine(sum);
}
}

pic 複製代碼 代碼如下:using System;
class Account
{
private double balance = 0; //欄位
public double Balance //屬性
{
get { return balance; }
set { balance = value;}
}
/*=============================================================
* 我們可以通過修改get、set方法達到控制存取的目的。
* 例如:
*
* 1)唯讀屬性
* public double Balance //屬性
* {
* get { return balance; }
* set { }
* }
*
* 2)讀寫控制
* public double Balance
* {
* get
* {
* if(Console.ReadLine()=="1234")
* return balance;
* else
* return -9999999;
* }
* set { }
* }
* =============================================================
*/
public void Deposit(double n)
{ this.balance += n; }
public void WithDraw(double n)
{ this.balance -= n; }
}
class Client
{
public static void Main()
{
Account a = new Account();
a.Balance = 1000; // 可以讀寫屬性,因為屬性Balance是public型的
//a.balance = 1000; //不可以讀寫欄位,因為欄位balance是private型的
a.WithDraw(500);
a.Deposit(2000);
Console.WriteLine(a.Balance);
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.