Valentino Vranic OOP 2015/16 Vlastnosti (properties) class TimePeriod { private double _seconds; public double Seconds { get { return _seconds; } set { _seconds = value; } } } Autoimplementácia vlastností class TimePeriod2 { public double Hours { get; set; } } Odlišná vnútorná reprezentácia vlastností class TimePeriod { private double seconds; public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } } } class Program { static void Main() { TimePeriod t = new TimePeriod(); // Assigning the Hours property causes the 'set' accessor to be called. t.Hours = 24; // Evaluating the Hours property causes the 'get' accessor to be called. System.Console.WriteLine("Time in hours: " + t.Hours); } } // Output: Time in hours: 24 Properties C# Programming Guide, http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx Zástupcovia (delegates) C# Reference, http://msdn.microsoft.com/en-us/library/900fyy8e.aspx // Declare delegate -- defines required signature: delegate double MathAction(double num); class DelegateTest { // Regular method that matches signature: static double Double(double input) { return input * 2; } static void Main() { // Instantiate delegate with named method: MathAction ma = Double; // Invoke delegate ma: double multByTwo = ma(4.5); Console.WriteLine("multByTwo: {0}", multByTwo); // Instantiate delegate with anonymous method: MathAction ma2 = delegate(double input) { return input * input; }; double square = ma2(5); Console.WriteLine("square: {0}", square); // Instantiate delegate with lambda expression MathAction ma3 = s => s * s * s; double cube = ma3(4.375); Console.WriteLine("cube: {0}", cube); } // Output: // multByTwo: 9 // square: 25 // cube: 83.740234375 } Anonymné metódy Anonymous Methods C# Programming Guide, http://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx // Create a delegate. delegate void Del(int x); // Instantiate the delegate using an anonymous method. Del d = delegate(int k) { /* ... */ }; // Create a handler for a click event. button1.Click += delegate(System.Object o, System.EventArgs e) { System.Windows.Forms.MessageBox.Show("Click!"); }; Rozšírujúce metódy (extension methods) Extension Methods (C# Programming Guide), https://msdn.microsoft.com/en-us/library/bb383977.aspx namespace ExtensionMethods { public static class MyExtensions { public static int WordCount(this String str) { return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length; } } } using ExtensionMethods; ... string s = "Hello Extension Methods"; int i = s.WordCount(); Menné priestory namespace SampleNamespace { class SampleClass { public void SampleMethod() { System.Console.WriteLine( "SampleMethod inside SampleNamespace"); } } } Namespaces C# Programming Guide, http://msdn.microsoft.com/en-us/library/0d941h9d.aspx Parciálne triedy a metódy namespace PM { partial class A { partial void OnSomethingHappened(string s); } // This part can be in a separate file. partial class A { // Comment out this method and the program // will still compile. partial void OnSomethingHappened(String s) { Console.WriteLine("Something happened: {0}", s); } } } partial (Method) (C# Reference), http://msdn.microsoft.com/en-us/library/6b0scde8.aspx Udalosti (events) using System; namespace wildert { public class Metronome { public event TickHandler Tick; public EventArgs e = null; public delegate void TickHandler(Metronome m, EventArgs e); public void Start() { while (true) { System.Threading.Thread.Sleep(3000); if (Tick != null) { Tick(this, e); } } } } public class Listener { public void Subscribe(Metronome m) { m.Tick += new Metronome.TickHandler(HeardIt); } private void HeardIt(Metronome m, EventArgs e) { System.Console.WriteLine("HEARD IT"); } } class Test { static void Main() { Metronome m = new Metronome(); Listener l = new Listener(); l.Subscribe(m); m.Start(); } } } The Simplest C# Events Example Imaginable, http://www.codeproject.com/Articles/11541/The-Simplest-C-Events-Example-Imaginable