Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. class Program
  2. {
  3. static void IncrementAge(ref Person person)
  4. {
  5. Console.WriteLine($"Inside function => {person.Name}'s age before change: {person.Age}");
  6. person.Age += 1;
  7. Console.WriteLine($"Inside function => {person.Name}'s age after change: {person.Age}");
  8. person = null;
  9. }
  10.  
  11. static void Main(string[] args)
  12. {
  13. var alice = new Person { Name = "Alice", Age = 17 };
  14. Console.WriteLine($"Inside main => {alice.Name}'s age before function call: {alice.Age}");
  15. IncrementAge(ref alice);
  16. if (alice != null)
  17. {
  18. Console.WriteLine($"Inside main => {alice.Name}'s age after function call: {alice.Age}");
  19. }
  20. else
  21. {
  22. Console.WriteLine($"Inside main => Object reference not set to an instance of an object");
  23. }
  24.  
  25. Console.ReadLine();
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement