Skip to content

Instantly share code, notes, and snippets.

@oze4
Last active January 22, 2019 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oze4/496ce3a8d9578f4221a8e701c5cbf6af to your computer and use it in GitHub Desktop.
Save oze4/496ce3a8d9578f4221a8e701c5cbf6af to your computer and use it in GitHub Desktop.
using System;
public class Program
{
public static void Main()
{
var currentCount = Person.GetCounter;
Console.WriteLine(string.Format("Current Person Count: {0}", currentCount));
var matt = new Person("matt", "lizzy", "pizzy");
var nick = new Person("nick", "yizzy", "fizzy");
Console.WriteLine(string.Format("Nick: {0} {1} {2}", nick.FirstName, nick.MiddleName, nick.LastName));
Console.WriteLine(string.Format("Matt: {0} {1} {2}", matt.FirstName, matt.MiddleName, matt.LastName));
var newCount = Person.GetCounter;
Console.WriteLine(string.Format("New Person Count: {0}", newCount));
}
public class Person
{
// this is a private field - only code in this class can access it
private static int _counter;
// public getter for our counter
public static int GetCounter { get { return _counter; } }
// private fields
private string _firstName;
private string _lastName;
// public getter/setter that let us manipulate private fields
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
// or you could just expose props publicly, and the data behind them, which isnt recommended
public string MiddleName { get; set; }
// class constructor - you can tell because the 'method' is named the same as the class
public Person(string fn, string ln, string mn)
{
FirstName = fn;
LastName = ln;
MiddleName = mn;
// incriment counter
_counter++;
}
}
}
/** OUTPUT:
Current Person Count: 0
Nick: nick fizzy yizzy
Matt: matt pizzy lizzy
New Person Count: 2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment