Properties
Properties are syntactic shorthand for accessor and mutator methods. In place of coding methods that retrieve or modify a particular member variable, properties provide both a get and a set submethod. Support for property style methods can be found in Visual Basic, C#, C++ (using __declspec()) and COM, to name a few. Consider the code snippet in Listing 2.2.
Listing 2.2. Get/Set Methods
1: class Person { 2: 3: private string name; 4: 5: public void SetName( string Name ) { name = Name; } 6: public string GetName( ) { return name; } 7: } 8: 9: Person me = new Person( ); 10: me.SetName( "Richard" ); 11: Console.WriteLine( me.GetName( ) ); |
The code in Listing 2.2 shows two methods whose sole purpose is to expose the underlying private ...
Get .NET Windows® Forms Custom Controls now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.