Use ref for reference type : ref « Language Basics « C# / CSharp Tutorial

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / CSharp Tutorial » Language Basics » ref 
1. 11. 6. Use ref for reference type
 

using System;


class MyClass
{
   public int Val = 20;                  
}

class MainClass
{
   static void MyMethod(ref MyClass myObject, ref int intValue)
   {
      myObject.Val = myObject.Val + 5;               
      intValue = intValue + 5;                       
   }

   static void Main()
   {
      MyClass myObject = new MyClass();
      int intValue = 10;

      Console.WriteLine("Before -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);

      MyMethod(ref myObject, ref intValue);          

      Console.WriteLine("After  -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
   }
}

        
Before -- myObject.Val: 20, intValue: 10
After  -- myObject.Val: 25, intValue: 15
  
1. 11. ref
1. 11. 1. Use ref to pass a value type by reference
1. 11. 2. Swap two values
1. 11. 3. Swap two references.
1. 11. 4. Use out for reference type
1. 11. 5. Use ref for int value
1. 11. 6. Use ref for reference type
1. 11. 7. Change string using ref keyword
w___ww.___ja_v_a2___s___.c___o_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.