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(); } }
Preprocessor 2
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ namespace nsPreProc { using System; public class PreProc { static public void Main() { #if ALTMAIN #warning Compiling alternate statement Console.WriteLine ("Using alternate Main()"); #elif OTHERMAIN #warning Compiling other statement Console.WriteLine ("Using other Main()"); #else #warning Compiling main Console.WriteLine ("Using Main()"); #endif } #line 200 #if SHOWERROR int iVar; #error This is line 23 but the error report should show line 202 #endif } }
Demonstrate #elif
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate #elif. #define RELEASE using System; public class Test5 { public static void Main() { #if EXPERIMENTAL Console.WriteLine("Compiled for experimental version."); #elif RELEASE Console.WriteLine("Compiled for release."); #else Console.WriteLine("Compiled for internal testing."); #endif #if TRIAL && !RELEASE Console.WriteLine("Trial version."); #endif Console.WriteLine("This is in all versions."); } }
Demonstrate #else
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate #else. #define EXPERIMENTAL using System; public class Test4 { public static void Main() { #if EXPERIMENTAL Console.WriteLine("Compiled for experimental version."); #else Console.WriteLine("Compiled for release."); #endif #if EXPERIMENTAL && TRIAL Console.Error.WriteLine("Testing experimental trial version."); #else Console.Error.WriteLine("Not experimental trial version."); #endif Console.WriteLine("This is in all versions."); } }
Use a symbol expression
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use a symbol expression. #define EXPERIMENTAL #define TRIAL using System; public class Test2 { public static void Main() { #if EXPERIMENTAL Console.WriteLine("Compiled for experimental version."); #endif #if EXPERIMENTAL && TRIAL Console.Error.WriteLine("Testing experimental trial version."); #endif Console.WriteLine("This is in all versions."); } }