c#学习笔记

示例程序
using System;
class HelloWorld
{
    static string say = “Hello World!”;
    static void Main(string[] args)
    {
    Console.WriteLine(say);
    }
}


值类型


   byte、sbyte、short、ushort、int、uint、long、ulong、char、float、double、decimal、bool、枚举、结构。


   字符串常用方法    
Compare 比较两个字符串
Format 格式化字符串的值
Trim 删除字符串中的空白
ToUpper / ToLower 改变字符串的大、小写
Split 分解为小字符串
IndexOf 第一次出现指定字符的索引
IndexOfAny 第一次出现任意字符的索引
Replace 用另一个字符(串)替换
Contains 是否包含有指定的字符串


引用类型


   Object、String、数组
   
枚举类型
 
enum Season
{
one,
two,
three
}

数组


     数据类型 [,] 数组名;






函数


属性
方法
构造函数
析构函数
运算符
索引


访问修饰符
public/protected/internal/protected internal/private


静态数据和静态方法
 private static int i = 0;
 static int methodName() {…}
 
 
方法的重载
 名字相同
 参数个数不同
 参数类型/类型不同
 与返回类型无关
 
 调用基类构造函数 base
 
 防止类被继承/方法被重载  sealed
 
 
 
 
Method(){…};
Method(int i){…};
Method(int j, string s){…};


多态
virtual/overide
 示例代码 (B和C 覆盖A中的方法)
  public class A{
     public virtual void methodTest(){
     
     } }
  public class B{
     public override void methodTest(){
     
     } }
  pulbic class c{
      public override void methodTest(){
     
     }}




抽象类和抽象方法
abstract public class A{}
abstract public void method(){}




接口
 interface iTestInterface{}




索引器


 语法格式


访问修饰符 返回类型 this[索引列表]
{
get{return;}
set{}
}
  注:可使用非数值下标
      可以重载
      不可以作为ref或者out参数使用
      
  
委托
public delegate double A(int a,int b);
  
  
  
事件   
访问修饰符 event 调用的委托名 事件名;


订阅事件
实例名.事件名 += 实例名.方法名;
取消订阅
实例名.事件名 -= 实例名.方法名;
引发事件
if(this.事件名 != null)
{
this.事件名();
}
  
  
io操作
FileStream outStream = null;
try{
    FileInfo bytesFile = new FileInfo(@”C:\fileName.dat”);
    outStream = bytesFile.OpenWrite();
    for(byte i = 0; i < 10; i++) {outStream.WriteByte(i);}

FileStream inStream = null;
try{
    FileInfo bytesFile = new FileInfo(@”C:\fileName.dat”);
    inStream = bytesFile.OpenRead();
    …
    for(int i = 0; i < inStream.Length; i++)
    {
        temp = inStream.ReadByte();
        Console.WriteLine(temp);
        totalSum += temp;
    }
 
 StreamWriter
 try{
    FileStream aFile = new FileStream(@”C:\fileName.txt”, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
sw.WriteLine(“Hello world!”);  
sw.Close();
    }
  
  StreamReader
  try{
    FileStream aFile = new FileStream(@”C:\fileName.txt”, FileMode.Open);
StreamReader sr = new StreamReader(aFile);
strLine = sr.ReadLine();
while(strLine != null)
{
Console.WriteLine(strLine);
strLine = sr.ReadLine();
}
sr.Close();
    }
  
  
命名空间(有点类似于java中的包名)
用.进行分隔
   语法格式
  namespace 命名空间名称
{
class 包含的类1{类成员}

class 包含的类n{类成员}
}

 引入命名空间
   using xxx.xxx


程序集
生成一个dll程序集


csc /out:MyName.dll /t:library MyName.cs


生成一个引用dll的exe程序集


csc /out: MyName.exe /r:TheOne.dll MyName.cs


 泛型
 public class A<T>



常用的集合类
arrayList
queue
stack
hashtable
sortedList
  
try
{
//可能有异常的代码片段
}
//可以有多个catch语句块
catch(Exception e) 
{
//异常发生时执行的代码
}
finally
{
//不论有无异常都要执行
}
posted @ 2012-09-06 22:34  retacn_yue  阅读(110)  评论(0编辑  收藏  举报