您的位置:首页 > 编程语言 > C#

C#学习笔记(二)

2010-10-18 16:34 246 查看

表达式

if(expression),exprsssion必须为bool值

checked和unchecked操作符用于整数算术运算时控制当前环境中的溢出检查。下列运算参与了check和unchecked检查:

预定义的++和-- 一元操作符,当其操作数类型为整数类型时。

预定义的 — 一元操作符,当其操作数为整数时。

预定义的+、—、*、/、等二元操作符,当两个操作数据类型都为整型。

从一种整型到另一种整型地显式数据转换。

//checked 和 unchecked检查
using System;
class Test
{
static int x = 1000000;
static int y = 1000000;
static int F()
{
return checked(x * y);     //运行错误,抛出异常
}
static int G()
{
return unchecked(x * y);   //返回值为 -727379968
}
static int H()
{
return x * y;         //依赖于编译时的默认情况
}
static void Main()
{
Console.WriteLine(F());
}
}


switch case语言:不容许遍历

using System;
class Test
{
static int x = 1000000;
static int y = 1000000;
static int F()
{
total = 365;
switch(month)
{
case 1:
total -= 31;
goto case 2;
case 2:
total -= 28;
goto case 3;
case 3:
total -= 30;
goto case 4;
case 4:
total -= 31;
goto case 5;
case 5:
total -= 30;
goto case 6;
case 6:
total -= 30;
goto case 7;
case 7:
total -= 31;
goto case 8;
case 8:
total -= 31;
goto case 9;
case 9:
total -= 30;
goto case 10;
case 10:
total -= 31;
goto case 11;
case 11:
total -= 30;
goto case 12;
case 12:
total -= 31;
goto default;
default:
total += day;
}
}
}


foreach 语句

/*fereach语句表示收集一个集合中的各元素,并针对各个元素执行内嵌语句。
语句的格式为:
foreach(type identifier in expression) embedded-statement
其中类型(type)和标识符(identify)用来声明循环变量,表达式
(expression)对应集合。
*************************************************************
只从foreach语句的使用角度举一个例子
假设Prime是一个满足条件的集合类型,它的元素类型为0到1000以内的
质数,MyInt是我们自定义的一个类型,其范围为200到300之间的整数。
************************************************************
用于在屏幕上打印出从200到300以内的所有质数 。 */
using System;
using System.Collections;
class Test()
{
public static void Main()
{
Console.WriteLine("See the prime number:");
foreach (MyInt x in Prime)
{
Console.WriteLine("{0}", x);
}
}
}


预编译指令

/*预编译和条件指令可以帮助我们在程序执行过程中发出编译的错误或警告
相应的指令是#warning和#error,下面的程序展示了他们的用法*******/
#define  DEBUG
#define  RELEASE
#define DEMO VERSION
#if DEMO VERSION && !DEBUG
#warning you are building a demo version
#endif
#if DEBUG && DEMO VERSION
#error you cannot build a debug demo version
#endif
using System;
class Demo
{
public static void Main()
{
Console.WriteLine("Demo application");
}
}
/*out:
在本例中,当你试图创建一个演示版时,会出现一个警告信息:
you are building a demo version,
当试图创建调试演示版时,会触发错误信息:
you cannot build a demo version.


异常处理

using System;
class Test
{
static void F()
{
try
{
G();
}
catch(Exception e)
{
Console.WriteLine("Exception in F: " + e.Message);
/*            e = new Exception("F");*/
/*            throw;*/
throw new Exception("F");
}
}
static void G()
{
throw new Exception("G");
}
static void Main()
{
try
{
F();
}
catch(Exception e)
{
Console.WriteLine("Exception in Main: " + e.Message);
}
}
}
/*out:
有注释时:
Exception in F: G
Exception in Main: G
无注释时:
Exception in F: G
Exception in Main: F
*/


类的声明

类的声明格式如下

attributes class-modifiers class identifier class-base class-body;

属性集 修饰符 类名 继承方式 基类名

类的修饰符

可以是下面几种之一或者是他们的组合

new——仅允许在嵌套类声明时使用,表明类中隐藏了由基类中继承而来的,与基类中同名的成员。

public——表示不限制对该类的访问。

protected——表示只能从所在类和所在类派生的子类进行访问。

internal——只有其所在类才能访问。

private——只有对包.Net中的应用程序或库才能访问

abstract——抽象类,不允许建立类的实例

sealed——密封类,不允许被继承

静态成员和非静态成员

类中的成员要么是静态,要么是非静态。一般来说,静态成员属于类所有的,非静态成员则属于类的实例——对象

//类的成员的访问修饰符的用法
using System;
class Test
{
int x;
static int y;
void F()
{
x = 1;   //ture equate to this.x = 1
y = 1;   //true equate to Test.y = 1;
}
static void G()
{
x = 1;     //false no access to this.x
y = 1;     //ture equate to Test.y = 1;
}
static void Main()
{
Test t = new Test();
t.x = 1;    //ture
t.y = 1;    //false,不能在类的实例中访问静态成员
Test.x = 1;//false,不能按类访问非静态成员
Test.y = 1; //ture
}
}


类的非静态成员属于类的实例所有,每创建一个类的实例,都在内存中为非静态成员开辟了一块区域。
而类的静态成员属于类所有,为这个类的所有实例所共享。无论这个类创建了多少个副本,一个静态成员在内存中只占有一块内存区域。

方法中的参数

C#中方法的参数有四中类型:

值参数,不含任何修饰符。

引用型参数,以ref修饰符声明。

输出参数,以out修饰符声明。

数组型参数,以params修饰符声明

输出参数

/*与引用型参数类似,输入型参数也不开辟新的内存区域。与引用型参数
* 的差别在于,调用方法前无需对变量进行初始化。输出型参数用于传递
* 方法返回的数据 out修饰符后应跟随与形参的类型相同的类型声明。
* 在方法返回后,传递的变量被认为经过了初始化。*/
using System;
class Test
{
static void SplitPath(string path, out string dir,
out string name )
{
int i = path.Length;
while(i > 0)
{
char ch = path[i - 1];
if (ch == '//' || ch == '/' || ch == ':')
break;
i--;
}
dir = path.Substring(0, i);
name = path.Substring(i);
}
static void Main()
{
string dir, name;
SplitPath("c://Windows//System//hello.txt", out dir, out name);
Console.WriteLine(dir);
Console.WriteLine(name);
}
}


数字型参数

如果形参表中包含了数字型参数,那么她它必须在参数表中位于最后。另外,参数只允许是一维数组。比如,string[]和string[][]都可以作为数字型参数,而string[,]不行,最后,数组型参数不能再有ref和out修饰符。

using System;
class Test
{
static void F(params int[] args)
{
Console.Write("Array contains {0} elements:", args.Length);
foreach (int i in args)
Console.Write(" {0}", i);
Console.WriteLine();
}
public static void Main()
{
int[] a = { 1, 2, 3 };
F(a);
F(10, 20, 30, 40);
F();
}
}


后两次调用完整的写法应该是:

F(new int[] {10, 20, 30, 40});

F(new int[] {});

使用成员方法重载操作符

重载一元操作符

/*角色类游戏中遇到的例子,内力,体力,经验值
* 剩余体力,剩余内力,5个属性。每当经验值到达
* 一定程度时,角色便会升级,体力,内力上升,剩余
* 体力和内力补满。“升级”使用的是重载操作符“++”
* 来实现的。   */
using System;
class Player
{
public int neili;
public int tili;
public int jingyan;
public int neili_r;
public int tili_r;
public Player()
{
neili = 10;
tili = 50;
jingyan = 0;
neili_r = 50;
tili_r = 50;
}
public static Player operator ++(Player p)
{
p.neili = p.neili + 50;
p.tili = p.tili + 100;
p.neili_r = p.neili;
p.tili_r = p.tili;
return p;
}
public void Show()
{
Console.WriteLine("Tili: {0}", tili);
Console.WriteLine("Jingyan: {0}", jingyan);
Console.WriteLine("Neili: {0}", neili);
Console.WriteLine("Tili_full: {0}", tili_r);
Console.WriteLine("Neili_full: {0}", neili_r);
}
}
class Test
{
public static void Main()
{
Player man = new Player();
man.Show();
man++;
Console.WriteLine("Now upgrading......");
man.Show();
}
}


重载二元操作符

/*笛卡尔坐标相加*/
using System;
class DKR
{
public int x, y, z;
public DKR(int vx, int vy, int vz)
{
x = vx;
y = vy;
z = vz;
}
public static DKR operator +(DKR d1, DKR d2)
{
DKR dkr = new DKR(0, 0, 0);
dkr.x = d1.x + d2.x;
dkr.y = d1.y + d2.y;
dkr.z = d1.z + d2.z;
return dkr;
}
}
class Test
{
public static void Main()
{
DKR d1 = new DKR(3, 2, 1);
DKR d2 = new DKR(0, 6, 5);
DKR d3 = d1 + d2;
Console.WriteLine("The 3d location of d3 is: {0},{1},{2}"
, d3.x, d3.y, d3.z);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: