码迷,mamicode.com
首页 > Windows程序 > 详细

C# 核心基础(1) 持续更新

时间:2016-08-14 13:07:22      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

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

            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
  • 变量必须初始化。否则,编译器就没有推断变量类型依据
  • 初始化器不能为空。
  • 初始化必须放在表达式中。
  • 不能把初始化器设置为一个对象,除非在初始化器中创建一个新对象。

一旦声明了变量,推断出了类型,不能改变变量类型。

变量作用域

  • 只要类在某个作用域内,其字段也在该作用域内。
  • 局部变量存在于声明该变量的语句块或方法结束的右花括号之前的作用域内。
  • 在for、while或类似语句中声明的局部变量存在于该循环体内。
局部作用域冲突

 只要变量的作用域是程序的不同部分,就不会有问题,也不会产生多义性。注意,同名局部变量不能再同一作用域内声明两次。

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

常量

  • 常量必须声明时初始化。执行值后,就不能再改写了。
  • 常量的值必须能在编译时用于计算。因此,不能从一个变量中提取的值用来初始化常量。(如果一定要这样做,可以用 readonly 只读)
  • 常量总是静态的,所以不能常量声明中包含修饰符 static
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 。当然值类型是不会有影响的。

CTS类型

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

流控制

if

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

switch

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
}

 

 

 

 

 

 

 

 

C# 核心基础(1) 持续更新

标签:

原文地址:http://www.cnblogs.com/z888/p/5769928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!