1. C# 基础语法和操作符
C# 中的运算符优先级
namespace OperatorsAppl
{ class Program7 { static void Main ( string [ ] args) { int a = 20 ; int b = 10 ; int c = 15 ; int d = 5 ; int e; e = ( a + b) * c / d; Console. WriteLine ( "(a + b) * c / d 的值是 {0}" , e) ; } }
}
2. 数据类型和控制结构
C# 中的变量定义和初始化
int i, j, k;
char c, ch;
float f, salary;
double d; int d = 3 , f = 5 ;
byte z = 22 ;
double pi = 3.14159 ;
char x = 'x' ;
C# 中的控制结构 - 条件语句
int a = 10 , b = 5 ;
string result = a > b ? "a大于b" : "a不大于b" ;
Console. WriteLine ( result) ;
C# 中的控制结构 - 循环
for ( int i = 0 ; i < 5 ; i++ )
{ Console. WriteLine ( i) ;
}
3. 字符串和数组操作
C# 字符串操作
string str = "Hello, World!" ;
Console. WriteLine ( str) ;
C# 数组操作
int [ ] array = new int [ 5 ] { 1 , 2 , 3 , 4 , 5 } ;
foreach ( var item in array)
{ Console. WriteLine ( item) ;
}
4. 函数和方法
C# 方法的定义和调用
namespace CalculatorApplication
{ class NumberManipulator { public int FindMax ( int num1, int num2) { return num1 > num2 ? num1 : num2; } static void Main ( string [ ] args) { NumberManipulator n = new NumberManipulator ( ) ; int max = n. FindMax ( 100 , 200 ) ; Console. WriteLine ( "最大值是: {0}" , max) ; } }
}
5. 类和对象
C# 类的定义和对象的创建
namespace BoxApplication
{ class Box { public double length; public double breadth; public double height; } class BoxTester { static void Main ( string [ ] args) { Box Box1 = new Box ( ) ; Box1. length = 6.0 ; Box1. breadth = 7.0 ; Box1. height = 5.0 ; Console. WriteLine ( "Box1 的体积: {0}" , Box1. length * Box1. breadth * Box1. height) ; } }
}
6. 继承和多态性
C# 继承
namespace InheritanceApplication
{ class Shape { protected int width, height; public void setWidth ( int w) { width = w; } public void setHeight ( int h) { height = h; } } class Rectangle : Shape { public int getArea ( ) { return width * height; } } class RectangleTester { static void Main ( string [ ] args) { Rectangle Rect = new Rectangle ( ) ; Rect. width = 5 ; Rect. height = 7 ; Console. WriteLine ( "总面积: {0}" , Rect. getArea ( ) ) ; } }
}
C# 多态性
namespace PolymorphismApplication
{ class Box { public double length, breadth, height; public Box ( double l, double b, double h) { length = l; breadth = b; height = h; } public double getVolume ( ) { return length * breadth * height; } } class Tester { static void Main ( string [ ] args) { Box Box1 = new Box ( 6.0 , 7.0 , 5.0 ) ; Box Box2 = new Box ( 12.0 , 13.0 , 10.0 ) ; Console. WriteLine ( "Box1 的体积: {0}" , Box1. getVolume ( ) ) ; Console. WriteLine ( "Box2 的体积: {0}" , Box2. getVolume ( ) ) ; } }
}
7. 接口和抽象类
C# 接口
interface IMyInterface
{ void MethodToImplement ( ) ;
}
class InterfaceImplementer : IMyInterface
{ public void MethodToImplement ( ) { Console. WriteLine ( "MethodToImplement() called." ) ; } static void Main ( ) { InterfaceImplementer iImp = new InterfaceImplementer ( ) ; iImp. MethodToImplement ( ) ; }
}
8. 异常处理
C# 异常处理
namespace ErrorHandlingApplication
{ class DivNumbers { int result; DivNumbers ( ) { result = 0 ; } public void division ( int num1, int num2) { try { result = num1 / num2; } catch ( DivideByZeroException e) { Console. WriteLine ( "Exception caught: {0}" , e) ; } finally { Console. WriteLine ( "Result: {0}" , result) ; } } static void Main ( string [ ] args) { DivNumbers d = new DivNumbers ( ) ; d. division ( 25 , 0 ) ; Console. ReadKey ( ) ; } }
}
9. 文件 I/O
C# 文件的输入与输出
using System ;
using System. IO ; namespace FileIOApplication
{ class Program { static void Main ( string [ ] args) { FileStream F = new FileStream ( "test.dat" , FileMode. OpenOrCreate, FileAccess. ReadWrite) ; for ( int i = 1 ; i <= 20 ; i++ ) { F. WriteByte ( ( byte ) i) ; } F. Position = 0 ; for ( int i = 0 ; i <= 20 ; i++ ) { Console. Write ( F. ReadByte ( ) + " " ) ; } F. Close ( ) ; Console. ReadKey ( ) ; } }
}
10. 正则表达式
C# 正则表达式
using System ;
using System. Text. RegularExpressions ; namespace RegExApplication
{ class Program { static void Main ( string [ ] args) { string input = "Hello World " ; string pattern = "\\s+" ; string replacement = " " ; Regex rgx = new Regex ( pattern) ; string result = rgx. Replace ( input, replacement) ; Console. WriteLine ( "Original String: {0}" , input) ; Console. WriteLine ( "Replacement String: {0}" , result) ; Console. ReadKey ( ) ; } }
}
11. 自定义异常
C# 创建用户自定义异常
using System ; using System ; namespace UserDefinedException
{ public class TempIsZeroException : ApplicationException { public TempIsZeroException ( string message) : base ( message) { } public TempIsZeroException ( ) { } public TempIsZeroException ( string message, Exception inner) : base ( message, inner) { } }
}