亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#常用知識(shí)點(diǎn)簡(jiǎn)單回顧(有圖有真相)

 更新時(shí)間:2013年01月11日 16:44:41   作者:  
C#知識(shí)點(diǎn)記錄編程的點(diǎn)點(diǎn)滴滴,本文整理了一些(傳值調(diào)用與引用調(diào)用/打印三角形/遞歸求階乘/多態(tài)性..等等),感興趣的朋友可以了解下的,不要走開(有圖有真相)
1)傳值調(diào)用與引用調(diào)用
復(fù)制代碼 代碼如下:

using System;
class MethodCall
{
public static void Main()
{
/*
* 參數(shù)類型分為 in, ref, out 三種,默認(rèn)為 in。
* in 類型在子方法中修改了對(duì)應(yīng)變量后,主方法中的值不會(huì)發(fā)生改變。
* ref 類型在子方法中修改了對(duì)應(yīng)變量后,主方法中的值也會(huì)發(fā)生改變。
* out 主方法中對(duì)應(yīng)的變量不需要初始化。
*
*/
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)打印三角形

復(fù)制代碼 代碼如下:

using System;
public class Hello
{
public static void Main()
{
Console.Write("請(qǐng)輸入行數(shù):");
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)遞歸求階乘

復(fù)制代碼 代碼如下:

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)多態(tài)性

復(fù)制代碼 代碼如下:

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(); //多態(tài)性決定著將調(diào)用Truck的Drive方法
Console.Read();
}
}

效果圖:

5)方法重載

復(fù)制代碼 代碼如下:

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)構(gòu)造函數(shù)

復(fù)制代碼 代碼如下:

using System;
public class Person
{
public string name = "";
public int age = 0;
//默認(rèn)構(gòu)造函數(shù)
public Person()
{
}
//構(gòu)造函數(shù)重載(1)
public Person(int Age)
{
this.age = Age;
}
//構(gòu)造函數(shù)重載(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)靜態(tài)與非靜態(tài)

復(fù)制代碼 代碼如下:

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()
{
//靜態(tài)方法調(diào)用應(yīng)當(dāng)使用 “類名.方法”
StaticHello.SayHello();
//非靜態(tài)方法調(diào)用應(yīng)當(dāng)使用 “實(shí)例名稱.方法”
NonStaticHello h = new NonStaticHello();
h.SayHello();
Console.Read();
}
}

效果圖:

8)九九表

復(fù)制代碼 代碼如下:

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)冒泡法排序

復(fù)制代碼 代碼如下:

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;
}
//輸出排序結(jié)果
foreach(int i in d)
Console.Write("{0}, ", i);
Console.Read();
}
}

效果圖:

10)求質(zhì)數(shù)

復(fù)制代碼 代碼如下:

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)

復(fù)制代碼 代碼如下:

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 接口只有一個(gè)方法: CompareTo。CompareTo方法
* 只接收一個(gè)object類型的參數(shù),這意味著它可以接收任何類
* 型的數(shù)據(jù)(object是所有類的父類),這個(gè)方法會(huì)返回一
* 整型數(shù)值,含義如下:
*
* 1) 小于零,當(dāng)前實(shí)例(this)小于obj對(duì)象
* 2) 等于零,當(dāng)前實(shí)例(this)等于obj對(duì)象
* 3) 大于零,當(dāng)前實(shí)例(this)大于obj對(duì)象
*
* Int32,Int16...,String,Decimal等數(shù)據(jù)類型都已經(jīng)實(shí)現(xiàn)了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, "排長(zhǎng)"));
list.Add(new Person(3, "團(tuán)長(zhǎng)"));
list.Add(new Person(4, "司令"));
list.Add(new Person(5, "旅長(zhǎng)"));
list.Add(new Person(7, "連長(zhǎng)"));
list.Add(new Person(1, "軍長(zhǎng)"));
list.Add(new Person(2, "營(yíng)長(zhǎng)"));
list.Add(new Person(8, "師長(zhǎng)"));
list.Sort();
Console.WriteLine("After Sorting");
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
}
}

效果圖:

12)使用接口排序(2)

復(fù)制代碼 代碼如下:

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 接口只有一個(gè)方法: CompareTo。CompareTo方法
* 只接收一個(gè)object類型的參數(shù),這意味著它可以接收任何類
* 型的數(shù)據(jù)(object是所有類的父類),這個(gè)方法會(huì)返回一
* 整型數(shù)值,含義如下:
*
* 1) 小于零,當(dāng)前實(shí)例(this)小于obj對(duì)象
* 2) 等于零,當(dāng)前實(shí)例(this)等于obj對(duì)象
* 3) 大于零,當(dāng)前實(shí)例(this)大于obj對(duì)象
*
* Int32,Int16...,String,Decimal等數(shù)據(jù)類型都已經(jīng)實(shí)現(xiàn)了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 == "軍長(zhǎng)")
return 7;
else if(rank == "師長(zhǎng)")
return 6;
else if(rank == "旅長(zhǎng)")
return 5;
else if(rank == "團(tuán)長(zhǎng)")
return 4;
else if(rank == "營(yíng)長(zhǎng)")
return 3;
else if(rank == "連長(zhǎng)")
return 2;
else
return 1;
}
#endregion
}
class SortArrayList
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person(6, "排長(zhǎng)"));
list.Add(new Person(3, "團(tuán)長(zhǎng)"));
list.Add(new Person(4, "司令"));
list.Add(new Person(5, "旅長(zhǎng)"));
list.Add(new Person(7, "連長(zhǎng)"));
list.Add(new Person(1, "軍長(zhǎng)"));
list.Add(new Person(2, "營(yíng)長(zhǎng)"));
list.Add(new Person(8, "師長(zhǎng)"));
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)屬性、方法作用范圍

復(fù)制代碼 代碼如下:

using System;
class Base
{
/*
* public 的可訪問范圍是所有類
* private 的可訪問范圍是當(dāng)前類
* protected 的可訪問范圍是當(dāng)前類及其子類
*/
public string name = "Tom";
private double salary = 1500;
protected int age = 20;
public virtual void ShowInfo()
{
Console.WriteLine(this.name); //可以,因?yàn)閚ame是 public 型的
Console.WriteLine(this.salary); //可以,salary是private型,在Base類中可以訪問
Console.WriteLine(this.age); //可以,因?yàn)閍ge是protected型,在子類中可以訪問
}
}
class Derived : Base
{
public override void ShowInfo()
{
Console.WriteLine(this.name); //可以,因?yàn)閚ame是 public 型的
//Console.WriteLine(this.salary); //不可以,salary是private型,超出Base就無法訪問
Console.WriteLine(this.age); //可以,因?yàn)閍ge是protected型,在子類中可以訪問
}
}
class Client
{
public static void Main()
{
Base b = new Base();
Console.WriteLine(b.name); //可以,因?yàn)閚ame是 public 型的
//Console.WriteLine(this.salary); //不可以,salary是private型,超出Base就無法訪問
//Console.WriteLine(this.age); //不可以,因?yàn)閍ge是protected型,Client不是Base的子類
Console.WriteLine("==========================");
b.ShowInfo();
Console.WriteLine("==========================");
Derived d = new Derived();
d.ShowInfo();
}
}

效果圖:

15)字段與屬性

復(fù)制代碼 代碼如下:

using System;
class SumToHundred
{
public static void Main()
{
int sum=0;
for(int i=1; i<=100; i++)
sum += i;
Console.WriteLine(sum);
}
}

pic
復(fù)制代碼 代碼如下:

using System;
class Account
{
private double balance = 0; //字段
public double Balance //屬性
{
get { return balance; }
set { balance = value;}
}
/*=============================================================
* 我們可以通過修改get、set方法達(dá)到控制存取的目的。
* 例如:
*
* 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; // 可以讀寫屬性,因?yàn)閷傩訠alance是public型的
//a.balance = 1000; //不可以讀寫字段,因?yàn)樽侄蝏alance是private型的
a.WithDraw(500);
a.Deposit(2000);
Console.WriteLine(a.Balance);
}
}

相關(guān)文章

  • C#利用雪花算法實(shí)現(xiàn)生成ID工具類

    C#利用雪花算法實(shí)現(xiàn)生成ID工具類

    雪花算法表示生成的ID如雪花般獨(dú)一無二,該算法源自Twitter,主要用于解決分布式系統(tǒng)的唯一Id生成問題,本文主要介紹了C#如何利用利用雪花算法實(shí)現(xiàn)生成ID,需要的可以參考下
    2023-12-12
  • c#之利用API函數(shù)實(shí)現(xiàn)動(dòng)畫窗體的方法詳解

    c#之利用API函數(shù)實(shí)現(xiàn)動(dòng)畫窗體的方法詳解

    本篇文章是對(duì)c#中利用API函數(shù)實(shí)現(xiàn)動(dòng)畫窗體的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • C#如何對(duì)Dictionary遍歷賦值

    C#如何對(duì)Dictionary遍歷賦值

    這篇文章主要介紹了C#如何對(duì)Dictionary遍歷賦值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Unity UI拖拽模型選擇功能

    Unity UI拖拽模型選擇功能

    這篇文章主要為大家詳細(xì)介紹了Unity UI拖拽模型選擇功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C#實(shí)現(xiàn)調(diào)用本機(jī)攝像頭實(shí)例

    C#實(shí)現(xiàn)調(diào)用本機(jī)攝像頭實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)調(diào)用本機(jī)攝像頭的方法,可以實(shí)現(xiàn)調(diào)用本機(jī)攝像頭進(jìn)行拍照,具有不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C#中進(jìn)程的掛起與恢復(fù)

    C#中進(jìn)程的掛起與恢復(fù)

    這篇文章主要介紹了C#中進(jìn)程的掛起與恢復(fù)操作方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • c# 實(shí)現(xiàn)雪花分形的示例

    c# 實(shí)現(xiàn)雪花分形的示例

    這篇文章主要介紹了c# 實(shí)現(xiàn)雪花分形的示例,幫助大家更好的利用c#繪制圖像,感興趣的朋友可以了解下
    2020-10-10
  • C#虛方法的聲明和使用實(shí)例教程

    C#虛方法的聲明和使用實(shí)例教程

    這篇文章主要介紹了C#虛方法的聲明和使用實(shí)例教程,對(duì)于初學(xué)者理解C#的虛方法有一定的幫助,需要的朋友可以參考下
    2014-07-07
  • 在WPF中使用多線程更新UI

    在WPF中使用多線程更新UI

    這篇文章介紹了在WPF中使用多線程更新UI的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#圖表算法之最短路徑

    C#圖表算法之最短路徑

    本文詳細(xì)講解了C#圖表算法之最短路徑,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04

最新評(píng)論