第2篇 C# 数据基本结构和运算
1、类、接口、Sub子程序和变量修饰符
类,以某种参数实例化后即成为对象。我这样理解:对象就是程序中的实体,一个UI或一个数据结构,类是一些实体的共有属性的集合。类可以通过XAML标记语言编程设计而定义,这种类一般用作UI或UI元素;类也可以通过C#代码编程设计而定义,通过C#代码,既可以定义用作UI或UI元素的类,可以定义数据结构的类。UI或UI元素的类不是本篇讨论的内容。
C#编程与汇编语言编程完全不同。后者是为CPU编写代码,程序是顺序结构,设计人员要用的是CPU。C#语言编程不是顺序结构,基本与CPU无关,用的是微软的操作系统,运行时,不是C#代码在运行,而是操作系统在运行,C#代码为操作系统提供一些任务,由微软操作系统完成。这些代码必须符合微软操作系统的规定,微软把这些规定商业包装成.Net,为便于编程者使用.net,微软设计了WPF、XAML、C#三个东西,你就是用这三样东西来设计你的应用程序。XAML用于设计UI,C#代码负责UI所体现的信息的处理。UI上产生“事件”,C#代码处理事件,微软称此对事件处理的程序为“方法”,方法就是一个个的sub子程序。
定义变量的场合因此有三处,类的内外,sub的内外。类型则有临时有效或永久有效。C#通过变量修饰符为变量设定类型。
1.1 类的属性修饰符
? internal。该修饰符声明类是内部的,仅本项目使用。可省略。
internal class MyClass { // Class members. }
? public。声明类是公共的,可由其他项目中的代码来访问。
public class MyClass { // Class members. }
? abstract。抽象类。不能实例化,只能继承。下例中public亦可为internal。
public abstract class MyClass { // Class members, may be abstract. }
? sealed。密封类。不能继承。下例中public亦可为internal。
下例为定义一个密封类。
public sealed class MyClass { // Class members. }
下例为使用一个密封类。
public class MyClass : MyBase { // Class members. }
注意,在C#的类定义中,只能有一个基类。如果继承了一个抽象类,该派生类除非也是抽象的,就必须实现继承该抽象类的所有抽象成员。编译器不允许派生类访问高于该基类的类。也就是说,内部类可继承于一个公共基类,但公共基类不能继承于一个内部类。
在类的继承层次结构中,所有的类的根都是System.Object。如果没有使用基类,则所定义的类就只继承于基类System.Object。
? 为类指定接口
语法:
[public] class MyClass :[ MyBase, ]IMyInterface[, IMySecondInterface][, ...] { // Class members. }
1.2 接口的属性修饰符
定义接口
[public ]interface IMyInterface { // Interface members. }
接口修饰符无abstract和sealed。接口无根。
接口继承。定义一个接口,该接口可以继承于多个接口。
public interface IMyInterface : IMyBaseInterface, IMyBaseInterface2 { // Interface members. }
1.2 变量的作用域关键字
1.2.1 全局变量和局部变量
2、变量
2.1
<type>可选的内容和含义
<type> | ALIAS FOR别名 | 类型 | |
sbyte | System.SByte | Integer | between −128 and 127 |
byte | System.Byte | Integer | between 0 and 255 |
short | System.Int16 | Integer | between −32768 and 32767 |
ushort | System.UInt16 | Integer | between 0 and 65535 |
int | System.Int32 | Integer | between −2147483648 and 2147483647 |
uint | System.UInt32 | Integer | between 0 and 4294967295 |
long | System.Int64 | Integer | between −9223372036854775808 and 9223372036854775807 |
ulong | System.UInt64 | Integer | between 0 and 18446744073709551615 |
TYPE | ALIAS FOR | MIN M | MAX M | MIN E | MAX E | APPROX MIN VALUE |
APPROX MAX VALUE |
float | System.Single | 0 | 224 | −149 | 104 | 1.5 × 10−45 | 3.4 × 1038 |
double | System.Double | 0 | 253 | −1075 | 970 | 5.0 × 10−324 | 1.7 × 10308 |
decimal | System.Decimal | 0 | 296 | −28 | 0 | 1.0 × 10−28 | 7.9 × 1028 |
TYPE | ALIAS FOR | ALLOWED VALUES | |
char | System.Char |
Single Unicode character 一个Unicode字符 |
stored as an integer between 0 and 65535 存储0~65535之间的整数 |
bool | System.Boolean | Boolean value | true or false |
string | System.String | A sequence of characters | 一组字符 |
原文:http://www.cnblogs.com/moiska/p/4910398.html