Sunday, February 26, 2012

C#






index  
C# Version 4.0

// Hello1.cs
public class Hello1
{
   public static void Main()
   {
      System.Console.WriteLine("Hello, World!");
   }
}

public Access is not restricted.
protected Access is limited to the containing class or types derived from the containing class.
internal Access is limited to the current assembly.
protected internal Access is limited to the current assembly or types derived from the containing class.
private Access is limited to the containing type.

static Declare a member that belongs to the type itself rather than to a specific object.
void  method does not return a value
Main method is the entry point of your program. It is declared inside a class or struct. It must be static. It can either have a void or int return type.  The Main method can be declared without parameters or with parameters. The latter form allows your program to read command-line arguments.
Source file file extension .cs
assembly: executable or library file
applications: file extension .exe (executable)
libraries: file extension dll (no Main method)

// Hello2.cs
using System;

public class Hello2
{
   public static void Main()
   {
      Console.WriteLine("Hello, World!");
   }
}

// Hello3.cs
// arguments: A B C D
using System;

public class Hello3
{
   public static void Main(string[] args)
   {
      Console.WriteLine("Hello, World!");
      Console.WriteLine("You entered the following {0} command line arguments:",
         args.Length );
      for (int i=0; i < args.Length; i++)     
         Console.WriteLine("{0}", args[i]);
     
   }
}

// Hello4.cs
using System;
public class Hello4
{
   public static int Main(string[] args)
   {
      Console.WriteLine("Hello, World!");
      return 0;
   }
}

System is a namespace
Namespaces contain types and other namespaces
Console is a class in the System namespace

<Namespace>.<Class>.<Method>(parameters);


using System;
namespace Acme.Collections
{
   public class Stack
   {
      Entry top;

      public void Push(object data) {
         top = new Entry(top, data);
      }

      public object Pop() {
         if (top == null) throw new InvalidOperationException();
         object result = top.data;
         top = top.next;
         return result;
      }

      class Entry
      {
         public Entry next;
         public object data;

         public Entry(Entry next, object data) {
            this.next = next;
            this.data = data;
         }
      }
   }
}

using System;
class Test
{
   static void Main() {
      int i = 123;
      object o = i;        // Boxing
      int j = (int)o;      // Unboxing
   }
}

static void Main(string[] args)
{
   if (args.Length == 0)
      Console.WriteLine("a");  
   else
      Console.WriteLine("b");  
}

static void Main(string[] args)
{
   int n = args.Length;
   switch (n) {
      case 0:
         Console.WriteLine("No arguments");
         break;
      case 1:
         Console.WriteLine("One argument");
         break;
      default:
         Console.WriteLine("{0} arguments", n);
         break;
      }
   }
}


No comments:

Post a Comment