枚举数组接口
2007-08-23 13:35
183 查看
看一下c#中如何定义一个枚举类型:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
internal enum Color ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
White, // Assigned a value of 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Red, // Assigned a value of 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Green, // Assigned a value of 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Blue, // Assigned a value of 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Orange // Assigned a value of 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
在看一下编译后的样子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
internal struct Color : System.Enum ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color White = (Color) 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color Red = (Color) 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color Green = (Color) 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color Blue = (Color) 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color Orange = (Color) 4;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//包含Color的变量值,不能写代码直接引用这个实例字段
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Int32 value__;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
在看一下几种不同的输出Color值的方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Color c = Color.Blue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c); //“Blue” 泛型格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c.ToString()); //“Blue” 泛型格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c.ToString("G")); //“Blue” 泛型格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c.ToString("D")); //“3”十进制格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c.ToString("X")); //“03”十六进制格式
c#中的System.Enum类为我们提供了好多操作枚举类型的方法,下面举一个例子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Color[] colors = (Color[]) Enum.GetValues(typeof(Color));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine("Number of symbols defined: " + colors.Length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine("Value Symbol ----- ------");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Chapter 12: Enumerated Types and Bit Flags 289
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
foreach (Color c in colors) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("{0,5:D} {0:G}", c);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Value Symbol
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--------- -----------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0 White
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
1 Red
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2 Green
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3 Blue
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
4 Orange
其他方法自己可以去尝试用一下。我们可以在枚举类型上应用位标记,这样在操作它的时候会进一步简单,譬如我们在写FileAttributes的时候可以这样写:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String file = @"C:Boot.ini";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FileAttributes attributes = File.GetAttributes(file);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine("Is {0} hidden? {1}", file,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
(attributes & FileAttributes.Hidden) == FileAttributes.Hidden);
下面看一下FileAttributes的定义:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[Flags, Serializable]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public enum FileAttributes ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Readonly = 0x0001,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hidden = 0x0002,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System = 0x0004,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Directory = 0x0010,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Archive = 0x0020,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Device = 0x0040,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Normal = 0x0080,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Temporary = 0x0100,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SparseFile = 0x0200,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ReparsePoint = 0x0400,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Compressed = 0x0800,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Offline = 0x1000,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NotContentIndexed = 0x2000,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Encrypted = 0x4000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//Flags特性代表位标记的意思,Serializable特性代表可序列化
我们也可以在自己的枚举类型上应用位标记,简化操作。
所有c#中的数组都隐式继承自System.Array类型,所有数组都隐式实现IEnumerable,ICollection,和IList接口。在对数组进行强制转换的时候要记住值类型的数组不许可被转换成任何其它类型。下面看个例子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FileStream[,] fs2dim = new FileStream[5, 10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Object[,] o2dim = fs2dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//错误,维数不同
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Stream[] s1dim = (Stream[]) o2dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Stream[,] s2dim = (Stream[,]) o2dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String[,] st2dim = (String[,]) o2dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Int32[] i1dim = new Int32[5];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//错误值类型不许转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Object[] o1dim = (Object[]) i1dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//可以借助Array.Copy实现转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Object[] ob1dim = new Object[i1dim.Length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Array.Copy(i1dim, ob1dim, i1dim.Length);
我们知道c#中的数组是基于0索引的,但是可以用Array.CreateInstance创建不是0索引的。但是不推荐这样做,因为不是基于0索引的数组效率低,而基于0索引的数组微软做了好多优化工作,效率会很高的。
我们知道c#中只能实现单继承,但是可以实现多个接口,这样也就隐式的实现了多继承。c#中有两种实现接口的方法,分别为显示实现和隐式实现,下面看个例子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public sealed class Program ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void Main() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SimpleType st = new SimpleType();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
st.Dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IDisposable d = st;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.Dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
internal sealed class SimpleType : IDisposable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void Dispose() ...{ Console.WriteLine("public Dispose"); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
void IDisposable.Dispose() ...{ Console.WriteLine("IDisposable Dispose"); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public Dispose //隐式接口的输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IDisposable Dispose //显示接口的输出,注意调用的方式!!
记住:在调用显示接口实现时一定要强制转换成接口类型才能使用。
下面看一下泛型接口和接口约束:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static class SomeType ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private static void Test() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Int32 x = 5;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Guid g = new Guid();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
M(x);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
M(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//以下调用非法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SomeType st=new SomeType();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
M(st);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//约束条件是:T类型必须实现 IComparable, IConvertible 这两个接口,否则不能调用M方法。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private static Int32 M<T>(T t) where T : IComparable, IConvertible ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
internal enum Color ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
White, // Assigned a value of 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Red, // Assigned a value of 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Green, // Assigned a value of 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Blue, // Assigned a value of 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Orange // Assigned a value of 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
在看一下编译后的样子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
internal struct Color : System.Enum ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color White = (Color) 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color Red = (Color) 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color Green = (Color) 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color Blue = (Color) 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const Color Orange = (Color) 4;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//包含Color的变量值,不能写代码直接引用这个实例字段
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Int32 value__;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
在看一下几种不同的输出Color值的方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Color c = Color.Blue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c); //“Blue” 泛型格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c.ToString()); //“Blue” 泛型格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c.ToString("G")); //“Blue” 泛型格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c.ToString("D")); //“3”十进制格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine(c.ToString("X")); //“03”十六进制格式
c#中的System.Enum类为我们提供了好多操作枚举类型的方法,下面举一个例子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Color[] colors = (Color[]) Enum.GetValues(typeof(Color));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine("Number of symbols defined: " + colors.Length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine("Value Symbol ----- ------");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Chapter 12: Enumerated Types and Bit Flags 289
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
foreach (Color c in colors) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("{0,5:D} {0:G}", c);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Value Symbol
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--------- -----------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0 White
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
1 Red
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2 Green
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3 Blue
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
4 Orange
其他方法自己可以去尝试用一下。我们可以在枚举类型上应用位标记,这样在操作它的时候会进一步简单,譬如我们在写FileAttributes的时候可以这样写:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String file = @"C:Boot.ini";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FileAttributes attributes = File.GetAttributes(file);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.WriteLine("Is {0} hidden? {1}", file,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
(attributes & FileAttributes.Hidden) == FileAttributes.Hidden);
下面看一下FileAttributes的定义:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[Flags, Serializable]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public enum FileAttributes ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Readonly = 0x0001,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hidden = 0x0002,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System = 0x0004,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Directory = 0x0010,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Archive = 0x0020,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Device = 0x0040,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Normal = 0x0080,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Temporary = 0x0100,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SparseFile = 0x0200,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ReparsePoint = 0x0400,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Compressed = 0x0800,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Offline = 0x1000,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NotContentIndexed = 0x2000,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Encrypted = 0x4000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//Flags特性代表位标记的意思,Serializable特性代表可序列化
我们也可以在自己的枚举类型上应用位标记,简化操作。
所有c#中的数组都隐式继承自System.Array类型,所有数组都隐式实现IEnumerable,ICollection,和IList接口。在对数组进行强制转换的时候要记住值类型的数组不许可被转换成任何其它类型。下面看个例子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FileStream[,] fs2dim = new FileStream[5, 10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Object[,] o2dim = fs2dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//错误,维数不同
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Stream[] s1dim = (Stream[]) o2dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Stream[,] s2dim = (Stream[,]) o2dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String[,] st2dim = (String[,]) o2dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Int32[] i1dim = new Int32[5];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//错误值类型不许转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Object[] o1dim = (Object[]) i1dim;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//可以借助Array.Copy实现转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Object[] ob1dim = new Object[i1dim.Length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Array.Copy(i1dim, ob1dim, i1dim.Length);
我们知道c#中的数组是基于0索引的,但是可以用Array.CreateInstance创建不是0索引的。但是不推荐这样做,因为不是基于0索引的数组效率低,而基于0索引的数组微软做了好多优化工作,效率会很高的。
我们知道c#中只能实现单继承,但是可以实现多个接口,这样也就隐式的实现了多继承。c#中有两种实现接口的方法,分别为显示实现和隐式实现,下面看个例子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public sealed class Program ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void Main() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SimpleType st = new SimpleType();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
st.Dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IDisposable d = st;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.Dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
internal sealed class SimpleType : IDisposable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void Dispose() ...{ Console.WriteLine("public Dispose"); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
void IDisposable.Dispose() ...{ Console.WriteLine("IDisposable Dispose"); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public Dispose //隐式接口的输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IDisposable Dispose //显示接口的输出,注意调用的方式!!
记住:在调用显示接口实现时一定要强制转换成接口类型才能使用。
下面看一下泛型接口和接口约束:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static class SomeType ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private static void Test() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Int32 x = 5;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Guid g = new Guid();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
M(x);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
M(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//以下调用非法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SomeType st=new SomeType();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
M(st);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//约束条件是:T类型必须实现 IComparable, IConvertible 这两个接口,否则不能调用M方法。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private static Int32 M<T>(T t) where T : IComparable, IConvertible ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 数组、集合的接口和枚举介绍
- 数组、集合的接口和枚举介绍
- JAVA基础语法、面向对象、了解java对象的生存周期(Object、修饰符、数组、枚举、接口、抽象类)
- 数组VS枚举;类VS结构;抽象类VS接口
- Java 接口中使用数组缺点的理由
- 【1】C#基础:数据类型、运算、随机、枚举、结构、数组、列表、文件和文件流、序列化、MD5
- 黑马程序员_二十三 【代理】【反射】【数组反射】【枚举】
- 6.数组 枚举 -C#高级编程
- iOS——之数组 字典 枚举
- SpringMVC接收数组,postman测试接口的方式
- 通过枚举控制比较TreeSet内容排序(实现接口comparator);随机生成一个汉字
- 数组,集合,IEnumerable接口,迭代器
- C#:枚举、结构与接口简单理解
- 一个动态数组堆栈模块的接口
- 枚举在接口的应用
- 1.0 基础、标示符、常量、数据类型(enum 枚举,struct 结构体)、操作符、循环、数组
- 东北大炸弹 宝岛地图-枚举+数组记录+前缀和
- C#总结【常量-枚举-数组】
- 枚举实现含有接口
- 离散化数组接口