Csharp/C Sharp/Language Basics/Ref Out — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:39, 26 мая 2010

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.rupare(sTest, "") == 0) {
            Console.WriteLine("sTest references an empty string");
            return;
        }
        Console.WriteLine("sTest refers to: "" + sTest + """);
    }
}


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\n" );
      SquareRef( ref y );  
      SquareOut( out z );  
      Console.WriteLine( "Value of y after SquareRef: {0}", y );
      Console.WriteLine( "Value of z after SquareOut: {0}\n", 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();
   }
}


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 };
   } 
}


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;
}