Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- */
Add Comment
Please, Sign In to add comment