Current area: HOME -> Blogs -> Jonathan's Blog -> Read Post

A wish for C# 4.0

Posted on Monday, February 04, 2008 at 8:25 AM
OK, OK, so C# 3.0 is only just out of the door. As anyone who has been reading my stuff for a while will have realized, I like what they've done with the language. Here at PH, we have now fully migrated all our solutions to Visual Studio 2008, and can now use C# 3.0 language and .Net 3.5 framework features everywhere they're useful. This makes me happy (though some annoying issues migrating to Web Deployment Projects 2008 decidedly aren't making me happy).

I haven't thought long and hard about what I'd really like in C# 4.0. I think I need C# 3.0 to settle in a bit more. I've used it quite a bit, but it takes a while to fully realize the power of all that is there, and I certainly don't want to start suggesting additions to the language when there is already a perfectly good solution to that problem.

There are many principles in programming language design. I'm quite heavily influenced by Larry Wall, designer of Perl, probably because I've listened to him talking about language design more than anyone else. One of the principles he has is known as huffmanization. Huffman coding is an approach to compression where you give the most frequently occurring things the shortest codes, so the things that occur most in the file you are compressing have the shortest representations. The analogy in programming language design is things that you do regularly should have a compact syntax, whereas things you do infrequently can be longer - because you do them less.

C# doesn't score badly overall alongside this principle generally, but there is one area that I really feel could use some improvement. How often do you write:
private string _Foo;
And then an accessor and mutator method:
public string Foo
{
    get { return _Foo; }
    set { _Foo = value; }
}
I find myself doing this a lot, and it's 6 lines of boilerplate (OK, only four lines if you ignore the braces, which don't have any understanding cost, but do take up screen space). By contrast, anonymous types let you declare fields and and accessors all in one go (and yes, I know it's a tad different).

What I'd really like is a way, in a single declaration, to declare an underlying private field with either protected, internal or public simple accessors. I'm not sure how it could look, or of some nice syntax for it. I guess an attribute could do it:
[Accessor("Foo", Scope.Public)]
private int _Foo;
Though that's still quite a bit to type. I've still not found any syntax to suggest that I like, though. Ideas, anyone? But anyway, I'd really like to see some improvement here. It's not a Big New Feature, but it would save a lot of trivial, boring code.

Comments
Accessor - Posted on Thursday, February 21, 2008 at 12:18 PM by Rudy
google for C# automatic properties


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Virtual File System SDK
Create your own file systems in Windows and .NET applications
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!


Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.