Advertisement
Guest User

Untitled

a guest
May 5th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. class Foo
  2. {
  3. public int a = 10;
  4. }
  5.  
  6. // out -> more or less like ref
  7. // difference = we don't need to initialize the object we pass beforehand
  8. static void getNameSurname(out string name, out string surname)
  9. {
  10. name = Console.ReadLine();
  11. surname = Console.ReadLine();
  12. }
  13.  
  14. // ref -> passes the reference so we modify the object itself
  15. static void PutAZero(ref Foo number)
  16. {
  17. number = null;
  18. }
  19.  
  20. static void Run(string[] args)
  21. {
  22. Foo x = new Foo();
  23. PutAZero(ref x);
  24. //Console.WriteLine(x.a); // Throws exception bc null
  25.  
  26. string n, s;
  27. getNameSurname(out n, out s); // With ref -> need to initialize, with out -> not
  28. Console.WriteLine("Name: " + n);
  29. Console.WriteLine("Surname: " + s);
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement