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) { Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(false)); Console.WriteLine("This OS has {0} object generations. ", (GC.MaxGeneration + 1)); Car refToMyCar = new Car("Zippy", 100); Console.WriteLine(refToMyCar.ToString()); Console.WriteLine(" Generation of refToMyCar is: {0}", GC.GetGeneration(refToMyCar)); } public static void MakeACar() { Car myCar = new Car(); } }
Author: coder
Estimated bytes on heap
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(false)); Console.WriteLine("This OS has {0} object generations. ", (GC.MaxGeneration + 1)); } }
Check the Generation for an object array
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(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(GC.GetGeneration(refToMyCar));
}
public static void MakeACar() {
Car myCar = new Car();
}
}
[/csharp]
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 & 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, &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, &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(); } } }