Last active
October 14, 2022 09:35
-
-
Save Havret/0ab7335cdb7edb7ca8c22212336b5bff to your computer and use it in GitHub Desktop.
Arguments - struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var alice = new Person { Name = "Alice", Age = 17 }; | |
Console.WriteLine($"Inside main => {alice.Name}'s age before function call: {alice.Age}"); | |
IncrementAge(alice); | |
if (alice != null) | |
{ | |
Console.WriteLine($"Inside main => {alice.Name}'s age after function call: {alice.Age}"); | |
} | |
else | |
{ | |
Console.WriteLine($"Inside main => Object reference not set to an instance of an object"); | |
} | |
void IncrementAge(Person person) | |
{ | |
Console.WriteLine($"Inside function => {person.Name}'s age before change: {person.Age}"); | |
person.Age += 1; | |
Console.WriteLine($"Inside function => {person.Name}'s age after change: {person.Age}"); | |
person = default; | |
} | |
public struct Person | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment