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

 更新时间:2013年01月11日 16:44:41   作者:  
C#知识点记录编程的点点滴滴,本文整理了一些(传值调用与引用调用/打印三角形/递归求阶乘/多态性..等等),感兴趣的朋友可以了解下的,不要走开(有图有真相)
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);
}
}

相关文章

  • winfrom 在业务层实现事务控制的小例子

    winfrom 在业务层实现事务控制的小例子

    winfrom 在业务层实现事务控制的小例子,需要的朋友可以参考一下
    2013-03-03
  • C# Pointer指针应用实例简述

    C# Pointer指针应用实例简述

    这篇文章主要介绍了C# Pointer指针应用,对初学者很有借鉴学习价值,需要的朋友可以参考下
    2014-08-08
  • Dictionary扩展基础类向字典中添加键和值

    Dictionary扩展基础类向字典中添加键和值

    Dictionary<TKey, TValue> 类是常用的一个基础类,但用起来有时确不是很方便。本文逐一讨论,并使用扩展方法解决
    2013-11-11
  • C#开发Winform程序调用存储过程

    C#开发Winform程序调用存储过程

    这篇文章介绍了C#开发Winform程序调用存储过程的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#数据导入到EXCEL的方法

    C#数据导入到EXCEL的方法

    今天小编就为大家分享一篇关于C#数据导入到EXCEL的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • C# 如何规范的写 DEBUG 输出

    C# 如何规范的写 DEBUG 输出

    本文来告诉大家一个规范,如何去写 DEBUG 的输出。本文给大家介绍的非常详细,需要的朋友参考下吧
    2017-12-12
  • 解析.NET中几种Timer的使用

    解析.NET中几种Timer的使用

    本文主要对.NET中4个Timer类,及其用法进行梳理,具有很好参考价值,需要的朋友一起来看下吧
    2016-12-12
  • c#使用file.copy实现文件备份示例

    c#使用file.copy实现文件备份示例

    需要把D盘Source文件夹中的所有名称包含"LTE"的子文件夹Copy到E盘的Backup文件中,实现特定文件夹每天备份,下面使用file.copy实现一下这个功能
    2014-03-03
  • 利用C#实现HTML模板的循环输出

    利用C#实现HTML模板的循环输出

    模板循环输出 ,是指使用 UI 前端设计的 HTML 模板片断,并结合数据记录进行循环输出的过程,本文将介绍如何中通过 C# 实现操作 HTML 模板的循环输出,文章通过代码示例讲解的非常详细,需要的朋友可以参考下
    2024-06-06
  • Unity游戏开发中的桥接模式

    Unity游戏开发中的桥接模式

    桥接模式是Unity游戏开发中常用的设计模式之一,用于将抽象部分与实现部分分离,从而使它们可以独立地变化。通过桥接模式,不同的抽象类可以与不同的实现类组合使用,从而实现更加灵活和可扩展的系统设计。常见的应用包括游戏中的场景渲染、UI界面设计、音效播放等
    2023-05-05

最新评论