maple.he 发表于 2016-8-10 17:10:47

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


效果图:http://files.jb51.net/file_images/article/201301/2013011116325138.jpg 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();
}
}


效果图:
http://files.jb51.net/file_images/article/201301/2013011116325139.jpg 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);
}
}


效果图:
http://files.jb51.net/file_images/article/201301/2013011116325140.jpg
http://t12.baidu.com/it/u=1580691470,2180138334&fm=76
免费云服务器

http://t12.baidu.com/it/u=714733328,4013637782&fm=76
c#教程

http://t12.baidu.com/it/u=1676011409,1192555680&fm=76
外贸网站

http://t12.baidu.com/it/u=1717719632,2079313811&fm=76
注册网站

http://t11.baidu.com/it/u=1496671384,950133907&fm=76
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();
}
}


效果图:
http://files.jb51.net/file_images/article/201301/2013011116325141.jpg5)方法重载
复制代码代码如下:

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


效果图:
http://files.jb51.net/file_images/article/201301/2013011116325142.jpg6)构造函数
复制代码代码如下:

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


效果图:
http://files.jb51.net/file_images/article/201301/2013011116325143.jpg7)静态与非静态
复制代码代码如下:

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


效果图:
http://files.jb51.net/file_images/article/201301/2013011116325144.jpg8)九九表
复制代码代码如下:

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


效果图:
http://files.jb51.net/file_images/article/201301/2013011116325145.jpg9)冒泡法排序
复制代码代码如下:

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<d)
{
temp = d;
d=d;
d=temp;
}
//输出排序结果
foreach(int i in d)
Console.Write("{0}, ", i);
Console.Read();
}
}


效果图:
http://files.jb51.net/file_images/article/201301/2013011116325146.jpg10)求质数
复制代码代码如下:

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


效果图:
页: [1]
查看完整版本: C#常用知识点简单回顾(有图有真相)