现在的位置: 首页 > 综合 > 正文

通过实例学习C#(1)

2013年03月03日 ⁄ 综合 ⁄ 共 2906字 ⁄ 字号 评论关闭
  1. (以下内容来自http://www.fincher.org/tips/Languages/csharpLINQ.shtml)

    The obligatory example for any language,(任何语言的必须的例子)注:你懂得,Hello World!

     
    using System;
    public class HelloWorld
    {
        public static void Main(string[] args) {
    	Console.Write("Hello World!");
        }
    }
    
  2. Raw CSharp compiler(原生C#编译器)

    You can compile c# using the command line version(你可以使用命令行工具来编译C#程序)

     
    C:>csc HelloWorld.cs
    

    and then run the new program by entering(然后,通过键入HelloWorld来运行程序)

    HelloWorld
    

    You can get Nant, a build tool like the old 'make', from http://sourceforge.net/projects/nant.(你可以下载Nant从http://sourceforge.net/projects/nant,一个像老的make的编译工具)

  3. Reading Stuff(读取相关)

    1. Read an entire file into a string(将整个文件读取到一个字符串中)

      using System;
      namespace PlayingAround {
          class ReadAll {
              public static void Main(string[] args) {
                  string contents = System.IO.File.ReadAllText(@"C:\t1");
                  Console.Out.WriteLine("contents = " + contents);
              }
          }
      }
      
    2. Read a file with a single call to sReader.ReadToEnd() using streams(单独调用流文件的ReadToEnd()函数来读取文件)

       
      public static string getFileAsString(string fileName) {
         StreamReader sReader = null;
         string contents = null;
         try {
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            sReader = new StreamReader(fileStream);
            contents = sReader.ReadToEnd();
         } finally {
           if(sReader != null) {
               sReader.Close();
            }
         }
         return contents;
      }
      
    3. Read all the lines from a file into an array(从文件中读取所有行到一个数组中)

       
      using System;
      namespace PlayingAround {
          class ReadAll {
              public static void Main(string[] args) {
                  string[] lines = System.IO.File.ReadAllLines(@"C:\t1");
                  Console.Out.WriteLine("contents = " + lines.Length);
                  Console.In.ReadLine();
              }
          }
      }
      
    4. Read a file line by line with no error checking(没有错误检测的一行一行读取文件)

      Useful if the file may be really large.(如果一个文件真的很大的话,是很有用的)

       
      StreamReader sr = new StreamReader("fileName.txt");
      string line;
      while((line= sr.ReadLine()) != null) {
      	Console.WriteLine("xml template:"+line);
      }
      
      if (sr != null)sr.Close();  //should be in a "finally" or "using" block
      
  4. Writing Stuff(写入相关)

    1. Easy writing all text to a file(简单的将所有文本写入到一个文件)

      WriteAllText will create the file if it doesn't exist, otherwise overwrites it. It will also close the file.(WriteAllText函数将会创建文件,如果文件不存在的话;否则会覆盖已经存在的文件。它也会关闭文件)

       
      using System;
      namespace PlayingAround {
          class ReadAll {
              public static void Main(string[] args) {
                  string myText = "Line1" + Environment.NewLine + "Line2" + Environment.NewLine;
                  System.IO.File.WriteAllText(@"C:\t2", myText);
              }
          }
      }
      
    2. Write a line to a file using streams(使用文件流将一行写入文件)

       
      using System;
      using System.IO;
      
      public class WriteFileStuff {
      
      public static void Main() {
             FileStream fs = new FileStream("c:\\tmp\\WriteFileStuff.txt", FileMode.OpenOrCreate, FileAccess.Write);			
             StreamWriter sw = new StreamWriter(fs);
             try {
      		sw.WriteLine("Howdy World.");
      	} finally {
      		if(sw != null) { sw.Close(); }
      	}
      }
      }
      
      
    3. Access files with "using"(通过using关键字来写入文件)

      "using" does an implicit call to Dispose() when the using block is complete. With files this will close the file. The following code shows that using does indeed close the file, otherwise 5000 open files would cause problems.(当using块完成之后,using会隐性的调用Dispose函数,这样会关闭这个文件。下面的代码显示了,using确实会关闭文件,否则5000次打开文件会导致问题发生的。)

      using System;
      using System.IO;

      class Test {
      private static void Main() {
      for (int i = 0; i < 5000; i++) {
      using (TextWriter w = File.CreateText("C:\\tmp\\test\\log"

抱歉!评论已关闭.