stackalloc Demo

using System;

class MainEntryPoint {
static unsafe void Main() {
Console.Write(“How big an array do you want?
> “);
string userInput = Console.ReadLine();
uint size = uint.Parse(userInput);

long* pArray = stackalloc long[(int)size];
for (int i = 0; i < size; i++) pArray[i] = i * i; for (int i = 0; i < size; i++) Console.WriteLine("Element {0} = {1}", i, *(pArray + i)); } } [/csharp]

You must declare that code that uses the sizeof operator as unsafe.

   
  
class SomeClass
{
    static unsafe public void ShowSizes() 
    {
        Console.WriteLine("
Basic type sizes");
        Console.WriteLine("sizeof short = {0}", sizeof(short));
        Console.WriteLine("sizeof int = {0}", sizeof(int));
        Console.WriteLine("sizeof long = {0}", sizeof(long));
        Console.WriteLine("sizeof bool = {0}", sizeof(bool));
    }
}
   
class SizeofBasicTypesApp
{
    unsafe public static void Main(string[] args) 
    {
        SomeClass.ShowSizes();
    }
}

   
     


Equals and ReferenceEquals


   


using System;
   
   
public class Name {
  public string firstName;
  public string lastName;
   
  public Name(string firstName, string lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
   
  public void Display() {
    Console.WriteLine("firstName = " + firstName);
    Console.WriteLine("lastName = " + lastName);
  }
   
  public static Name Copy(Name car) {
    return (Name) car.MemberwiseClone();
  }
}
   
   
class Test {
  public static void Main() {
    Name myName = new Name("T", "M");
    Name myOtherName = new Name("P", "B");
    Console.WriteLine("myName details:");
    myName.Display();
    Console.WriteLine("myOtherName details:");
    myOtherName.Display();
   
    // set the myName object reference equal to myOtherName
    Console.WriteLine("Setting myName equal to myOtherName");
    myName = myOtherName;
   
    // check for equality
    Console.WriteLine("Name.Equals(myName, myOtherName) ="+ Name.Equals(myName, myOtherName));
    Console.WriteLine("Name.ReferenceEquals(myName, myOtherName) ="+ Name.ReferenceEquals(myName, myOtherName));
   
  }
   
}
           
          


compare the difference between passing a null reference vs. a reference to a zero length string

   
 

using System;

public class Class1 {
    public static void Main(string[] strings) {
        Example exampleObject = new Example();

        Console.WriteLine("Pass a null object:");
        string s = null;
        exampleObject.TestString(s);
        Console.WriteLine();

        Console.WriteLine("Pass an empty string:");
        exampleObject.TestString("");
        Console.WriteLine();

        Console.WriteLine("Pass a real string:");
        exampleObject.TestString("test string");
        Console.WriteLine();

    }
}

class Example {
    public void TestString(string sTest) {
        if (sTest == null) {
            Console.WriteLine("sTest is null");
            return;
        }

        if (String.Compare(sTest, "") == 0) {
            Console.WriteLine("sTest references an empty string");
            return;
        }
        Console.WriteLine("sTest refers to: &#039;" + sTest + "&#039;");
    }
}

    


the out descriptor allows a function a value in an argument without initializing the argument


   
 
using System;

public class Test {
    public static void Main(string[] strings) {
        Student student;
        Example example = new Example();
        example.ReturnStudent(out student);

        Console.WriteLine("Student is " + student.name);
    }
}

class Example {
    public void ReturnStudent(out Student student) {
        student = new Student();
        student.name = "Jenny";
    }
}

public class Student {
    public string name;
}

    


Testing the effects of passing array references by value and by reference.

using System;

public class ArrayReferenceTest
{
public static void Main( string[] args )
{
int[] firstArray = { 1, 2, 3 };
int[] firstArrayCopy = firstArray;
for ( int i = 0; i < firstArray.Length; i++ ) Console.Write( "{0} ", firstArray[ i ] ); FirstDouble( firstArray ); for ( int i = 0; i < firstArray.Length; i++ ) Console.Write( "{0} ", firstArray[ i ] ); if ( firstArray == firstArrayCopy ) Console.WriteLine("same" ); else Console.WriteLine("different" ); int[] secondArray = { 1, 2, 3 }; int[] secondArrayCopy = secondArray; for ( int i = 0; i < secondArray.Length; i++ ) Console.Write( "{0} ", secondArray[ i ] ); SecondDouble( ref secondArray ); for ( int i = 0; i < secondArray.Length; i++ ) Console.Write( "{0} ", secondArray[ i ] ); if ( secondArray == secondArrayCopy ) Console.WriteLine("same" ); else Console.WriteLine("different" ); } public static void FirstDouble( int[] array ) { for ( int i = 0; i < array.Length; i++ ) array[ i ] *= 2; array = new int[] { 11, 12, 13 }; } public static void SecondDouble( ref int[] array ) { for ( int i = 0; i < array.Length; i++ ) array[ i ] *= 2; array = new int[] { 11, 12, 13 }; } } [/csharp]