标签:
using System; namespace ConsoleApplicationCShape { class Program { static void Main(string[] args) { Console.WriteLine("Hello Wrox."); Console.ReadLine(); return; } } }
int i = 1; bool mBool = false; int x = 12, y = 12;
var name = "Aaron"; var age = 12; var isRabbit = false; Type nameType = name.GetType(); Type ageType = age.GetType(); Type isRabbitType = isRabbit.GetType(); Console.WriteLine("name is type " + nameType.ToString()); Console.WriteLine("age is type " + ageType.ToString()); Console.WriteLine("isRabbit is type " + isRabbitType.ToString()); Console.ReadLine();
输出
name is type System.String age is type System.Int32 isRabbit is type System.Boolean
一旦声明了变量,推断出了类型,不能改变变量类型。
只要变量的作用域是程序的不同部分,就不会有问题,也不会产生多义性。注意,同名局部变量不能再同一作用域内声明两次。
int j = 0; for (int i = 0; i < 10; i++) { int j = 0; Console.WriteLine(i + j); }
for (int i = 0; i < 10; i++) { int j = 0; Console.WriteLine(i + j); } for (int i = 0; i < 10; i++) { int j = 0; Console.WriteLine(i + " " + j); }
字段 (静态变量)
static int j = 20
局部变量
int j = 30
static int j = 20; static void Main(string[] args) { int j = 30; Console.WriteLine(j); Console.WriteLine(Program.j); Console.ReadLine(); }
const int j = 20;
C#数据类型分为 值类型 和 引用类型
值类型直接存储其值,引用类型存储对值得引用。
值类型存储在堆栈中,引用类型存储在托管堆上。
值类型
int x = 20; int j = 30;
引用类型
Vector x, y; x = new Vector(); x.X = 30; Console.WriteLine(x); y = x; y.X = 30; Console.WriteLine(y); Console.WriteLine(x);
输出
30,0 30,0 30,0
注意 我这里 Vector 只有在 System.Windows.WindowsBase 命名空间中,大家要测试引用类型时,需创建WPF程序。
要创建对象,须使用new 关键字。因为 x 和 y 引用同一对象,所以修改 x,也会影响 y 。当然值类型是不会有影响的。
C# 有15个预定定义类型,其中13个是值类型,2个引用类型(string 和 object)
8个整数类型 sbyte、short、int、long、byte、ushort、uint、ulong
2个浮点类型 float、double
1个高精度浮点数 128位 decimal
1个布尔类型 bool
1个单字符类型 char
2个预定义引用类型 object、string
声明 decimal
decimal d = 12.30M;
声明 char
char c = ‘x‘;
声明 string
string str = "Hello world";
转义字符
string str1 = "C:\\Program Files (x86)\\ADSafe"; string str2 = @"C:\Program Files (x86)\ADSafe";
甚至包含换行符
string str2 = @"C:\Program Files (x86) \ADSafe"; Console.WriteLine(str2);
int intInput = Int32.Parse(Console.ReadLine()); if (intInput == 10) { Console.WriteLine("Input 10"); }else if (intInput == 20) { Console.WriteLine("Input 20"); } else { Console.WriteLine("Input Error"); } Console.ReadLine();
如果条件分支只有一条语句,就无须使用花括号:
if (intInput == 10) Console.WriteLine("Input 10");
while (true) { int intInput = Int32.Parse(Console.ReadLine()); switch (intInput) { case 0: case 10: Console.WriteLine("Input 10"); break; case 20: Console.WriteLine("Input 20"); goto case 30; break; case 30: Console.WriteLine("Input 30"); break; default: Console.WriteLine("Input Error"); break; } }
break 结束当前循环
continue 退出当前迭代,进行下一轮迭代
for
for (int i = 0; i < 10; i++) { Console.WriteLine(i); }
i++ ==> i = i + 1 ==> i += 1
while
int i = 0; while (i < 10) { i++; Console.WriteLine(i); }
do while
int i = 100; do { i++; Console.WriteLine(i); } while (i < 10);
foreach
int[] arr = new int[6] {1,2,3,4,5,6}; foreach (int value in arr) { Console.WriteLine(value); }
跳转语句 goto、break、continue、return
public enum TimeOfDay { Morning = 0, Afternoon = 1, Evening = 2 }
标签:
原文地址:http://www.cnblogs.com/z888/p/5769928.html