Skip to content

Instantly share code, notes, and snippets.

@Havret
Last active October 14, 2022 09:35
Show Gist options
  • Save Havret/0ab7335cdb7edb7ca8c22212336b5bff to your computer and use it in GitHub Desktop.
Save Havret/0ab7335cdb7edb7ca8c22212336b5bff to your computer and use it in GitHub Desktop.
Arguments - struct
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