Advertisement
Fhernd

RefExample.cs

Sep 27th, 2014
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. class RefExample{
  2.       static object FindNext(object value, object[] data, ref int index){
  3.             // NOTE: index can be used here because it is a ref parameter
  4.             while (index < data.Length){
  5.                   if (data[index].Equals(value)){
  6.                         return data[index];
  7.                   }
  8.                    index += 1;
  9.             }
  10.              return null;
  11.       }
  12.       static void Main(){
  13.             object[] data = new object[] {1,2,3,4,2,3,4,5,3};
  14.             int index = 0;
  15.             // NOTE: must assign to index before passing it as a ref parameter
  16.             while (FindNext(3, data, ref index) != null){
  17.                   // NOTE: that FindNext may have modified the value of index
  18.                   System.Console.WriteLine(“Found at index {0}”, index);
  19.                   index += 1;
  20.             }
  21.             System.Console.WriteLine(“Done Find”);
  22.       }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement