欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#常用知识点简单回顾(有图有真相)

程序员文章站 2023-12-11 23:30:22
1)传值调用与引用调用 复制代码 代码如下: using system; class methodcall { public static void main() { /*...
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;
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

 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();
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

 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);
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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();
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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;
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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();
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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();
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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();
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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();
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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;
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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);
}
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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);
}
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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();
}
}

效果图:

C#常用知识点简单回顾(有图有真相)

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);
}
}

上一篇:

下一篇: