Skip to content

Instantly share code, notes, and snippets.

@kierunb
Created September 19, 2022 11:22
Show Gist options
  • Save kierunb/7606d803947b40eafb700b169266d051 to your computer and use it in GitHub Desktop.
Save kierunb/7606d803947b40eafb700b169266d051 to your computer and use it in GitHub Desktop.
Fantastic snippet
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