Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RefExample{
- static object FindNext(object value, object[] data, ref int index){
- // NOTE: index can be used here because it is a ref parameter
- while (index < data.Length){
- if (data[index].Equals(value)){
- return data[index];
- }
- index += 1;
- }
- return null;
- }
- static void Main(){
- object[] data = new object[] {1,2,3,4,2,3,4,5,3};
- int index = 0;
- // NOTE: must assign to index before passing it as a ref parameter
- while (FindNext(3, data, ref index) != null){
- // NOTE: that FindNext may have modified the value of index
- System.Console.WriteLine(“Found at index {0}”, index);
- index += 1;
- }
- System.Console.WriteLine(“Done Find”);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement