A Proposed Alternative and Simplified C# Property Syntax
Jan 12, 2003 -
© Scott Zimmerman
says the first time they see a for loop in C/C++/Java/C#? As practical as the for loop is, it also proves the point that the syntax of a language rules! So just as the syntax of an enum is different from that of a struct, the property keyword is similar in that it also establishes special rules for the syntax that follows within the braces. The automatic (free) getter/setter in the simplified property syntax can be easily extended by replacing either or both with a function, as C# currently requires. But either way, you still have the option to avoid creating an unnecessary identifier for this common programming task. For example: ///////////////////////// public int property Myval { // usually we don't need a special getter, so one will be appointed for us get; // if we need a special setter, we do it without an extra variable set { if (value > 10) throw new Exception(); else // within the scope of a property, the compiler "knows" // that assignment is not a recursive call... Myval = value; } } ///////////////////////// I hope the C# community will speak up about this, and further refine the concept, so Microsoft and ECMA will consider this for future versions of the language. The views expressed in this articles is that of Scott Zimmerman's and does not necessarily reflect the views of Suite101.com or Jose Aniceto. The author also acertains that the copyright to this article belongs to the author.
The copyright of the article A Proposed Alternative and Simplified C# Property Syntax in C# Programming is owned by Scott Zimmerman. Permission to republish A Proposed Alternative and Simplified C# Property Syntax in print or online must be granted by the author in writing.
Go To Page: 1 2 Articles in this Topic Discussions in this Topic |