quark: Wishes for future versions of C# part 2:
August 29, 2004
Wishes for future versions of C# part 2
I’ve written about wishes for future versions of C# before, but I have a couple of more wishes I’d like to add, so here goes.
Simplified access modifier syntax
In C#, you need to specify the access modifier of a given property, field or method for each and every property, field and method in your class. E.g.:
public class MyClass { private int myint; private bool mybool; public string MyStringField; public int MyInt; public bool MyBool; private string mystringproperty; public string MyStringProperty { get { return this.mystringproperty; } set { this.mystringproperty = value; } } public void DoStuff() { // Do stuff }}
In C++, you can group fields, properties and methods with the same accessibility level like this:
private: string mystring; int myint; bool mybool;public: string MyString; int MyInt; bool MyBool;
In C#, this type of grouping is not possible. I would like this kind of grouping in C# as well, but not with the ugly colon syntax from C++. This is how I would like to see it in C#:
public class MyClass { private { int myint; bool mybool; string mystringproperty; } public { string MyStringField; int MyInt; bool MyBool; string MyStringProperty { get { return this.mystringproperty; } set { this.mystringproperty = value; } } void DoStuff() { // Do stuff } }}
This would tidy up the code a bit and make it easier to find the local and private fields, properties and methods when you need them, as well as all the other fields, properties and methods with the same accessibility level. It would also make it easy to expand the accessibility level group you’d like to have a look at, and contract the ones you don’t in the IDE.
Custom type validation at compile time
It’s fairly easy to create custom types in C#. It’s even simpler to do custom validation on types in getters and setters for a given class. But all this validation happens in runtime and not in compile time. Let’s first take a look at how you do validation in C# today.
Custom type validation
public struct EmailAddress { private static string emailstring; private EmailAddress(string value) { // A weak regex for e-mail addresses Regex emailRegex = new Regex(@"(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})$"); if (!emailRegex.IsMatch(value)) { throw new ArgumentException(string.Format( "The assigned string value \"{0}\"is not a valid e-mail address!", value)); } emailstring = value; } // EmailAddress public static implicit operator EmailAddress(string value) { return new EmailAddress(value); } public override string ToString() { return emailstring; }} // public struct EmailAddress
Setter validation
private string emailaddress;public string EmailAddress { get { return this.emailaddress; } set { Regex emailRegex = new Regex(@"(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})$"); if (!emailRegex.IsMatch(value)) { throw new ArgumentException(string.Format( "The assigned string value \"{0}\"is not a valid e-mail address!", value)); } this.emailaddress = value; }} // string EmailAddress
How it could be
What would be nice to see, is something like this:
private string emailaddress;[CustomValidationAttribute(@"(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})$"),"The assigned string value is not a valid e-mail address!"]public string EmailAddress { get { return this.emailaddress; } set { this.emailaddress = value; }} // string EmailAddress
With this attribute, it should be possible to validate input in compile time, and it would simplify the code very much for ad hoc types. E-mail address is probably not a good example for such an ad hoc type, but nonetheless, I think this type of functionality is very useful.
The with
keyword
Visual Basic makes me sick to my stomach, but it has some nice features. One of them is the with
keyword. I’d like to see it in a future version of C#, implemented like this:
with (MyObject) { .Property1 = value1; .Property2 = value2; .Property3 = value3;}Posted by asbjorn at August 29, 2004 04:16 PM
0 Comments:
Post a Comment
<< Home