摘要:委派即可以调用静态类方法,也可以调用对象方法。如下面的类Person定义了两个私有域来存储一个人的名字和年龄
阅读全文
随笔分类 - C#
摘要:可以使用一个委派调用多个方法,委派和方法都有一个限制:委派和方法都必须返回void。
下面的例子定义了一个叫DelegateCalculation的返回值为void的派生类:
public delegate void DelegateCalculation(
double acceleration, double time
);
阅读全文
摘要:委托就像一个函数的指针,在程序运行时可以使用它们来调用不同的函数。委托存储方法名,以及返回值类型和参数列表。一个委派有两个部分:委派类和类的委派对象。
定义委派类,如 public delegate double DelegateCalculation(
double acceleration ,double time
);
阅读全文
摘要:笔刷用Pen类表示,使用Pen后,必须释放资源。如下例
阅读全文
摘要:下面的例子演示了如何使用Color结构的Alpha成分,首先从Color结构中创建两个Color实例,一个是c1,它是蓝色的,其alpha值为100,另一个是绿色的alpha值为50.接着绘制三个实心图形
阅读全文
摘要:堆栈对象Stack和数据结构中的堆栈对象一样,即从顺序表的一段插入,并从这一段取出,可理解为一堆盘子,只能从盘子的上面增加和拿走盘子。
阅读全文
摘要:排序列表用SortedList对象表示,用Add()方法会自动将元素插入到适当的位置以保持关键字的顺序
阅读全文
摘要:Hashtable()为哈希表,可以在保存值的同时保存关键字,便于以后搜索,如存储美国州名的同时存储州的简写,如简写为"CA" ,州名为"California",其有Add,Clear,Clone,CopyTo,ContainsKey等方法:
阅读全文
摘要:数组列(ArrayList)与数组类似,但在给它增加元素时,能够自动扩展,而数组则有固定大小,其属于System.Collections命名空间的一部分。
它有Add,AddRange,Insert,InsertRange,Sort等方法,可理解为动态数组。
阅读全文
摘要:通过对类定义个索引器可以把对象的域当做一个数组元素。例如在Car类中定义一个索引器,用来读写make和model域,定义一个类myCar
myCar[0]访问make,myCar[1]访问model。
阅读全文
摘要:齿形数组时数组的一类,它的行也是数组---行数组中的元素个数可以不同。如string[][] names;接着names=new string[4][],创建了四个数组,它们的元素时字符串数组names[0]=new string[3]names[0][0]="Jason";names[0][1]="Marcus";names[0][2]="Price";names[1]=new string[2];names[0][0]="Steve";names[0][1]="Smith";例:/* Exa
阅读全文
摘要:/* Example10_8.cs illustrates the use of a three-dimensional rectangular array*/using System;class Example10_8{ public static void Main() { // create the galaxy array int[,,] galaxy = new int [10, 5, 3]; // set two galaxy array elements to the star's brightness galaxy[1, 3, 2] = 3; galaxy[4, 1,
阅读全文
摘要:C#中定义一个数组的方法一般为:DataType[]Array=newDataType[Size];其中DataType可以是基本数据类型和对象类型数据,Size是数组元素的个数.例子定义一个int类型的数据来表示一个班上的人数int[]classNum=newint[20];数组的属性有:属性意义publicvirtualboolIsFixedSize数组的长度是否是固定的publicvirtualboolIsReadOnly数组是否是只读型publicvirtualboolIsSynchronized在多线程环境下是否安全publicintLength数组元素的个数publicintRan
阅读全文
摘要:/* Example9_4.cs illustrates the use of DateTime properties and methods*/using System;class Example9_4{ public static void Main() { // use the Now and UtcNow properties to get the currrent // date and time Console.WriteLine("DateTime.Now = " + DateTime.Now); Console.WriteLine("DateTim
阅读全文
摘要:using System;using System.Linq;using System.Collections.Generic;class Exapmple1_1{ static void Main() { string[] names={"Burke","Connor","Frank", "Everett","Albert","George"}; IEnumerable<string> expr = from s in names where s.Length
阅读全文
摘要:/* Example9_3.cs illustrates the use of DateTime and TimeSpan instances*/using System;class Example9_3{ public static void DisplayDateTime( string name, DateTime myDateTime ) { Console.WriteLine(name + " = " + myDateTime); // display the DateTime's properties Console.WriteLine(name + &
阅读全文
摘要:System.Text.StringBuilder可以动态创建字符串。同String类中一般的字符串不同,动态字符串的字符可以直接修改,在一般的字符串中,修改的总是字符串的拷贝。同时StringBuilder方法效率更高并且提供了更有效的字符串操作能力。 /* Example9_2.cs illustrates the use of StringBuilder objects*/using System;using System.Text;class Example9_2{ public static void DisplayProperties( string name, StringBui
阅读全文
摘要:如下面的IDrivable和IStreerable都声明了TurnLeft()方法
public interface IDrivable
{
void TurnLeft();
}
public interface IStreerable
{
void TurnLeft();
}
阅读全文
摘要:接口间可用:实现服用,如public interface IMovable : IDrivable, ISteerable,例如
阅读全文