1. // # hello world
using System; namespace HelloWorldApplication {class HelloWorld{static void Main(string[] args) {/*my first C# program*/Console.WriteLine("HelloWorld C#");Console.ReadKey();}} }
2. // C# 计算矩形的面积
/*计算矩形的面积 x*y = area*/using System;
namespace RectangleApplication
{class Rectangle {//成员变量double X; //changdouble Y; //kuanpublic void Accpetdetails(){X = 1.2;Y = 1.3;}public double GetArea(){return X * Y;}public void dispaly() {Console.WriteLine("X:{0}", X);Console.WriteLine("Y:{0}", Y);Console.WriteLine("Area:{0}", GetArea());}class ExecuteRectangle{static void Main(string[] args){Rectangle r = new Rectangle();r.Accpetdetails();r.dispaly();Console.ReadLine();}}}
}
3. // 强制类型转换1
/*强制类型装换例子 11520.822 --->>>115200*/using System;namespace TypeconversationApplication
{class ExplicitConversation {static void Main(string[] agrs){double d = 115200.822;int i;//强制将double类型转换成int 类型i = (int) d;Console.WriteLine(i);Console.ReadKey();}}
}
4. /*强制类型转换多个例子:
整型、双精度、单精度、布尔类型*/
/*强制类型转换多个例子: 整型、双精度、单精度、布尔类型*/ using System;namespace TypeconversationApplication {class ExplicitConversation {static void Main(string[] agrs){int i = 75;float f = 53.00f;double d = 115200.82;bool b = true;/*int i;//强制将double类型转换成int 类型i = (int) d;*/Console.WriteLine(i.ToString());Console.WriteLine(f.ToString());Console.WriteLine(d.ToString());Console.WriteLine(b.ToString());Console.ReadKey();}} }
5.// C#变量初始化
using System;namespace TypeconversationApplication {class Program{static void Main(string[] args){short a;int b;double c;/*实际初始化*/a = 10;b = 20;c = a+b;Console.WriteLine("a ={0},b={1},c={2}", a, b, c);Console.ReadLine();}} }
6./*
常量是使用const关键字来定义的,
下面代码演示了如何在程序中定义和使用常量;*/
using System;public class ConstTest {class SampleClass{public int x;public int y;public const int c1 = 5;public const int c2 = c1 + 5;public SampleClass(int p1, int p2){x = p1;y = p2;}}static void Main(){SampleClass mC = new SampleClass(11, 22);Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);Console.WriteLine("c1 = {0}, c2 = {1}",SampleClass.c1, SampleClass.c2);} }
7./*
a++ ++a b-- --b 左右自增 、自减
*/
using System;namespace OperatorsAppl {class Program{static void Main(string[] args){int a = 1;int b;// a++ 先赋值再进行自增运算b = a++;Console.WriteLine("a = {0}", a);Console.WriteLine("b = {0}", b);Console.ReadLine();// ++a 先进行自增运算再赋值a = 1; // 重新初始化 ab = ++a;Console.WriteLine("a = {0}", a);Console.WriteLine("b = {0}", b);Console.ReadLine();// a-- 先赋值再进行自减运算a = 1; // 重新初始化 ab = a--;Console.WriteLine("a = {0}", a);Console.WriteLine("b = {0}", b);Console.ReadLine();// --a 先进行自减运算再赋值a = 1; // 重新初始化 ab = --a;Console.WriteLine("a = {0}", a);Console.WriteLine("b = {0}", b);Console.ReadLine();}} }
8.//关系运算符
using System;class Program {static void Main(string[] args){int a = 21;int b = 10;if (a == b){Console.WriteLine("Line 1 - a 等于 b");}else{Console.WriteLine("Line 1 - a 不等于 b");}if (a < b){Console.WriteLine("Line 2 - a 小于 b");}else{Console.WriteLine("Line 2 - a 不小于 b");}if (a > b){Console.WriteLine("Line 3 - a 大于 b");}else{Console.WriteLine("Line 3 - a 不大于 b");}/* 改变 a 和 b 的值 */a = 5;b = 20;if (a <= b){Console.WriteLine("Line 4 - a 小于或等于 b");}if (b >= a){Console.WriteLine("Line 5 - b 大于或等于 a");}} }
9.//逻辑运算符
using System;class Program {static void Main(string[] args){int a = 21;int b = 10;if (a == b){Console.WriteLine("Line 1 - a 等于 b");}else{Console.WriteLine("Line 1 - a 不等于 b");}if (a < b){Console.WriteLine("Line 2 - a 小于 b");}else{Console.WriteLine("Line 2 - a 不小于 b");}if (a > b){Console.WriteLine("Line 3 - a 大于 b");}else{Console.WriteLine("Line 3 - a 不大于 b");}/* 改变 a 和 b 的值 */a = 5;b = 20;if (a <= b){Console.WriteLine("Line 4 - a 小于或等于 b");}if (b >= a){Console.WriteLine("Line 5 - b 大于或等于 a");}} }
10.//if、else if、else逻辑判断嵌套用法
using System;namespace OperatorApp1 {class program{static void Main(string[] args){//局部变量的定义int a = 100;//检查布尔的条件if (a == 50){Console.WriteLine("a 的值为50");}else if (a == 60){Console.WriteLine("a 的值为60");}else if (a == 70){Console.WriteLine("a的值为70");}else{Console.WriteLine("以上条件没有匹配a的值");}Console.WriteLine("a的值是{0}",a);Console.ReadLine();}} }
11.//运用Switch case的思想 枚举星期一 到星期天的案例
using System;namespace MyApplication {class Program{static void Main(string[] args){int day = 3;switch (day){case 1:Console.WriteLine("Monday");break;case 2:Console.WriteLine("Tuesday");break;case 3:Console.WriteLine("Wednesday");break;case 4:Console.WriteLine("Thursday");break;case 5:Console.WriteLine("Friday");break;case 6:Console.WriteLine("Saturday");break;case 7:Console.WriteLine("Sunday");break;}}} }
12.// switch 嵌套的案例
using System;namespace MyApplication {class Program{static void Main(string[] args) {int a = 100;int b = 200;switch (a){case 100:Console.WriteLine("这是外部Switch的一部分");switch (b){case 200:Console.WriteLine("这是内部Switch的一部分");break;}break;}Console.WriteLine("a的准确值是{0}", a);Console.WriteLine("b的准确值是{0}", b);Console.ReadLine();}} }
13.//while 循环
using System;namespace MyApplication {class Program{static void Main(string[] args) {/*局部变量的定义*/int a = 10;/*while循环执行*/while(a<20) { Console.WriteLine(a);a++;}Console.ReadLine();}} }
14. //for循环
using System;namespace MyApplication {class Program{static void Main(string[] args) {/* for 循环执行 */for (int a = 10; a < 20; a ++){Console.WriteLine("a 的值: {0}", a);}Console.ReadLine();}} }
15. //0-100以内的质数
using System;namespace Loops {class Program{static void Main(string[] args){/* 局部变量定义 */int i, j;for (i = 2; i < 100; i++){for (j = 2; j <= (i / j); j++)if ((i % j) == 0) break; // 如果找到,则不是质数if (j > (i / j))Console.WriteLine("{0} 是质数", i);}Console.ReadLine();}} }
16. //调用方法,取最大值
using System;namespace CaculatorApplication {class NumberManipulator{public int FindMax(int num1, int num2){/* 局部变量声明 */int result;if (num1 > num2)result = num1;elseresult = num2;return result;}static void Main(string[] args){/* 局部变量定义 */int a = 100;int b = 200;int ret;NumberManipulator n = new NumberManipulator();//调用 FindMax 方法ret = n.FindMax(a, b);Console.WriteLine("最大值是: {0}", ret);Console.ReadLine();}} }
@源于www.runoob.com