class MyAwesomeClass { public int MyProperty { get; set; } } class Program { static void Main(string[] args) { MyAwesomeClass instance1 = new MyAwesomeClass(); MyAwesomeClass instance2 = new MyAwesomeClass(); instance1.MyProperty = 100; instance2.MyProperty = 200; // instance1.MyProperty is still 100 } }
class MyAwesomeClass { public void PrintText(string text) { Console.WriteLine(text); } } class Program { static void Main(string[] args) { MyAwesomeClass instance = new MyAwesomeClass(); instance.PrintText("Hello World"); // Outputs "Hello World" MyAwesomeClass.PrintText("Hello World"); // Results in an error } }
MyProperty
static
class MyAwesomeClass { public static int MyProperty { get; set; } = 100; } class Program { static void Main(string[] args) { MyAwesomeClass instance = new MyAwesomeClass(); Console.WriteLine(MyAwesomeClass.MyProperty); // Outputs "100" Console.WriteLine(instance.MyProperty); // Results in an error } }
class Person { public static int totalPersons = 0; private string name; public Person(string personName) // Person Constructor { name = personName; ++totalPersons; } public void PrintInfo() { Console.WriteLine("This person is called " + name + "."); Console.WriteLine("There are " + totalPersons + " persons total."); } }
class Program { static void Main(string[] args) { Person steve = new Person("Steve"); Person wendy = new Person("Wendy"); steve.PrintInfo(); wendy.PrintInfo(); } }
class Program { void PrintHelloName(string name) { Console.WriteLine("Hello, " + name); } static void Main(string[] args) { PrintHelloName(); // Will throw an error } }
static class Styling { public static string fontFamily = "Verdana"; public static float fontSize = 12.5f; } class Program { static void Main(string[] args) { Console.WriteLine ("Using font " + Styling.fontFamily + " " + Styling.fontSize + "px"); // Outputs "Using font Verdana 12.5px" Styling = new Styling(); // Results in an error } }
Message
int TotalMessages
string LastMessage
string MessageText
string message
TotalMessages
LastMessage
message
Message newMessage = new Message(message);
newMessage
allMessages
Message.TotalMessages
Message.LastMessage