using System;
class MainClass
{
// unsafe not required for primitive types.
static void Main()
{
Console.WriteLine("The size of short is {0}.", sizeof(short));
Console.WriteLine("The size of int is {0}.", sizeof(int));
Console.WriteLine("The size of long is {0}.", sizeof(long));
}
}
Month: February 2011
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: '" + sTest + "'");
}
}
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]
Reference, output and value parameters.
using System;
class ReferenceAndOutputParameters
{
public void aMethod()
{
int y = 5;
int z;
Console.WriteLine( "Original value of y: {0}", y );
Console.WriteLine( "Original value of z: uninitialized
" );
SquareRef( ref y );
SquareOut( out z );
Console.WriteLine( "Value of y after SquareRef: {0}", y );
Console.WriteLine( "Value of z after SquareOut: {0}
", z );
Square( y );
Square( z );
Console.WriteLine( "Value of y after Square: {0}", y );
Console.WriteLine( "Value of z after Square: {0}", z );
}
void SquareRef( ref int x )
{
x = x * x;
}
void SquareOut( out int x )
{
x = 6;
x = x * x;
}
void Square( int x )
{
x = x * x;
}
}
class ReferenceAndOutputParamtersTest
{
static void Main( string[] args )
{
ReferenceAndOutputParameters test = new ReferenceAndOutputParameters();
test.aMethod();
}
}

