日期:2013-01-12  浏览次数:20487 次

C# Codeing Examples
--------------------------------------------------------------------------------
Provided by Jeffrey Richter
Jeffrey Richter (JeffreyRichter.com) is a cofounder of Wintellect (Wintellect.com), a software consulting, education, and development firm that specializes in .NET, Windows, and COM programming. Jeffrey is also a contributing editor to MSDN Magazine and is the author of two Microsoft Press books: Programming Applications for Microsoft Windows and Programming Server-Side Applications for Microsoft Windows.

Jeffrey is a design and programming consultant for many companies including Microsoft, Intel, and DreamWorks. He is currently writing a book tentatvively titled: Programming Applications for the .NET Frameworks.

The following represents the short bits of code-fragments that Jeffrey had with him in order to illustrate various aspects of C# that he felt might be usefull to communicate during this episode. While we didn't get a chance to work through many of these examples, he graciously has provided us with his notes so that you might be able to look at them and get some additional ideas about the features and capabilities of C#.

Members Only
Everything is a member of a type. There are no global methods or variables. Note that the following "Hello World" sample application defines its "Main" function as part of a class:
using System;

class App {
   public static void Main(string[] args) {
      Console.WriteLine("Hello World");
   }
}

For everything, there is an exception
Exception support in C# is a first-class citizen of the language itself. The following example illustrates the use of the try/catch/finally process for detecting and reacting to errors. Note that the "finally" condition is guaranteed to be run regardless of the error situation.

Also notice the string @"C:\NotThere.txt", this illustrates the use of a '"Verbatim String" (That is what the "@" signifies) in which you don't need to worry about putting in two \\'s in order too get it to display one.
using System;
using System.IO;

public class App {
   public static void Main() {
      FileStream fs = null;
      try {
         fs = new FileStream(@"C:\NotThere.txt", FileMode.Open);
      }
      catch (Exception e) {
         Console.WriteLine(e.Message);
      }
      finally {
         if (fs != null) fs.Close();
      }
   }
}

Getting off to a good start
This example shows a few different aspects of how to pre-initialize values:
class MyCollection {
   Int32 numItemsInCollection = 0;
   DateTime creationDateTime = new DateTime.Now();

   // This even works for static fields
   static Int32 numObjInstances = 0;
   ...
}

Filling all your buckets
This example shows various aspects of defining, initializing, and using arrays.
static void ArrayDemo() {
   // Declare a reference to an array
   Int32[] ia; // defaults to null
   ia = new Int32[100];
   ia = new Int32[] { 1, 2, 3, 4, 5 };

   // Display the array's contents
   foreach (Int32 x in ia)
      Console.Write