开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 2974|回复: 0

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

[复制链接]

0

精华

-4

贡献

48

赞扬

帖子
39
软币
293
在线时间
22 小时
注册时间
2015-8-24
发表于 2016-8-10 17:10:47 | 显示全部楼层 |阅读模式
1)传值调用与引用调用
[url=]复制代码[/url]代码如下:

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)打印三角形
[url=]复制代码[/url]代码如下:

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)递归求阶乘
[url=]复制代码[/url]代码如下:

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


效果图:


[backcolor=rgba(0, 0, 0, 0.6)]免费云服务器



[backcolor=rgba(0, 0, 0, 0.6)]c#教程



[backcolor=rgba(0, 0, 0, 0.6)]外贸网站



[backcolor=rgba(0, 0, 0, 0.6)]注册网站



[backcolor=rgba(0, 0, 0, 0.6)]c#知识点











4)多态性
[url=]复制代码[/url]代码如下:

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)方法重载
[url=]复制代码[/url]代码如下:

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)构造函数
[url=]复制代码[/url]代码如下:

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)静态与非静态
[url=]复制代码[/url]代码如下:

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)九九表
[url=]复制代码[/url]代码如下:

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)冒泡法排序
[url=]复制代码[/url]代码如下:

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


效果图:
10)求质数
[url=]复制代码[/url]代码如下:

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赞扬 +1 收起 理由
higoku1 + 1 很给力

查看全部评分

回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-30 11:00

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表