If object array is still alive

using System;
using System.Collections.Generic;
using System.Text;

public class Car {
private int currSp;
private string petName;

public Car() { }
public Car(string name, int speed) {
petName = name;
currSp = speed;
}

public override string ToString() {
return string.Format(“{0} is going {1} MPH”,
petName, currSp);
}
}
class Program {
static void Main(string[] args) {
Car refToMyCar = new Car(“Zippy”, 100);
Console.WriteLine(refToMyCar.ToString());

Console.WriteLine(”
Generation of refToMyCar is: {0}”, GC.GetGeneration(refToMyCar));

object[] tonsOfObjects = new object[50000];
for (int i = 0; i < 50000; i++) tonsOfObjects[i] = new object(); GC.Collect(0); GC.WaitForPendingFinalizers(); Console.WriteLine("Generation of refToMyCar is: {0}",GC.GetGeneration(refToMyCar)); if (tonsOfObjects[9000] != null) { Console.WriteLine("Generation of tonsOfObjects[9000] is: {0}",GC.GetGeneration(tonsOfObjects[9000])); } else Console.WriteLine("tonsOfObjects[9000] is no longer alive."); } public static void MakeACar() { Car myCar = new Car(); } } [/csharp]

Use FormsAuthentication

   

using System;
using System.Web.Security;

//MD5Test

class Class1 {

    static void Main(string[] args) {
        string Brian = FormsAuthentication.HashPasswordForStoringInConfigFile("MyPassword", "MD5");
        Console.WriteLine(Brian);
        Brian = FormsAuthentication.HashPasswordForStoringInConfigFile("MyPassword", "sha1");
        Console.WriteLine(Brian);

    }
}

           
          


Demonstrate compound modifiers and calls to external API


   

/*
C# Programming Tips &amp; Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
//  GetUser.cs -- demonstratec compound modifiers and calls to external API
//
//                Compile this program with the following command line:
//                    C:>csc /unsafe GetUser.cs
//
namespace nsUser
{
    using System;
    using System.Runtime.InteropServices;
    
    public class GetUser
    {
        [DllImport ("user32.dll")]
        static extern public int MessageBox(int hWnd, string msg,
                                            string title, int type);
        [DllImport ("advapi32.dll")]
        static unsafe extern public bool GetUserName(byte [] User,
                                                     long *size);
        [DllImport ("advapi32.dll")]
        static unsafe extern public bool GetUserNameW(char [] User,
                                                     long *size);
        static public void Main ()
        {
            byte [] user = new byte[256];
            long size = (long) user.Length;
            unsafe
            {
                if (GetUserName (user, &amp;size) == false)
                    Console.WriteLine ("Error getting user name");
            }
            string strUser = "";
            foreach (byte ch in user)
            {
                if (ch == 0)
                    break;
                strUser += (char) ch;
            }
            MessageBox (0, "The current user is " + strUser, "Howdy", 0);
            /*
            //********************************************
            // The following code uses the wide-character version GetUserNameW()
            //
            strUser = "";
            char [] cUser = new char[256];
            size = cUser.Length;
            unsafe
            {
                if (GetUserNameW (cUser, &amp;size) == false)
                    Console.WriteLine ("Error getting user name");
            }
            foreach (char ch in user)
            {
                if (ch == 0)
                    break;
                strUser += ch;
            }
            MessageBox (0, "The current user is " + strUser, "Howdy", 0);
            */
        }
    }
}

           
          


GetHostEnumerator and GetAssemblyEnumerator

   


using System;
using System.Reflection;
using System.Collections;
using System.Security.Policy;

public class MainClass {
    public static void Main(string[] args) {
        Assembly a = Assembly.LoadFrom(args[0]);
        Evidence e = a.Evidence;

        IEnumerator x = e.GetHostEnumerator();
        Console.WriteLine("HOST EVIDENCE COLLECTION:");
        while (x.MoveNext()) {
            Console.WriteLine(x.Current.ToString());
            Console.WriteLine("Press Enter to see next evidence.");
            Console.ReadLine();
        }
        x = e.GetAssemblyEnumerator();
        Console.WriteLine("ASSEMBLY EVIDENCE COLLECTION:");
        while (x.MoveNext()) {
            Console.WriteLine(x.Current.ToString());
            Console.WriteLine("Press Enter to see next evidence.");
            Console.ReadLine();
        }

    }
}
           
          


Subclass EventArgs

   
 


using System;


[Serializable]
public sealed class MailReceivedEventArgs : EventArgs {
    private readonly string from;
    private readonly string subject;

    public MailReceivedEventArgs(string from, string subject) {
        this.from = from;
        this.subject = subject;
    }

    public string From { get { return from; } }
    public string Subject { get { return subject; } }
}

public class MainClass {
    public static void Main() {
        MailReceivedEventArgs args = new MailReceivedEventArgs("D", "Y");
        Console.WriteLine("From: {0}, Subject: {1}", args.From, args.Subject);
    }
}

    


Is 2003 Vista Or Greater

   
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


  internal class OSHelper
  {
    internal static bool Is2003VistaOrGreater { get { return Environment.OSVersion.Version.CompareTo(new Version(5, 2)) >= 0; } }
    internal static bool IsVistaOrGreater { get { return Environment.OSVersion.Version.CompareTo(new Version(6, 0)) >= 0; } }
  }