Created
September 19, 2022 11:22
-
-
Save kierunb/7606d803947b40eafb700b169266d051 to your computer and use it in GitHub Desktop.
Fantastic snippet
This file contains hidden or 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
| using System; | |
| public class Person | |
| { | |
| // Constructor that takes no arguments: | |
| public Person() | |
| { | |
| Name = "unknown"; | |
| } | |
| // Constructor that takes one argument: | |
| public Person(string name) | |
| { | |
| Name = name; | |
| } | |
| // Auto-implemented readonly property: | |
| public string Name { get; } | |
| // Method that overrides the base class (System.Object) implementation. | |
| public override string ToString() | |
| { | |
| return Name; | |
| } | |
| } | |
| class TestPerson | |
| { | |
| static void Main() | |
| { | |
| // Call the constructor that has no parameters. | |
| var person1 = new Person(); | |
| Console.WriteLine(person1.Name); | |
| // Call the constructor that has one parameter. | |
| var person2 = new Person("Sarah Jones"); | |
| Console.WriteLine(person2.Name); | |
| // Get the string representation of the person2 instance. | |
| Console.WriteLine(person2); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment