Kevin Dente's Blog

The Blip in the Noise
.NET naming conventions and protected members

While I've been doing a bunch of .NET development over the last year, I haven't really developed any class libraries until now (been web services and web application stuff, mostly). Being a good .NET citizen, I set the CLSCompliant attribute. I was coding merrily along, using the C# coding standard that I've adopted - camel case for members, Pascal case for properties and methods. Everything was ducky until I tried to add a protected field and a corresponding public accessor property. The compiler complained that this wasn't CLS compliant, since case-insensitive languages like VB.NET can't differentiate between the starts-with-lower-case field and the corresponding starts-with-upper-case property. Doh! Not sure why I never thought of that. The .NET framework style guide recommends camel case for protected fields, but I guess they didn't consider the case where there's a corresponding public property when they wrote that recommendation.

My first thought was to switch to the other common style that I've seen (but that I've never been crazy about) - prefixing fields with an underscore. No love - that's also not CLS compliant. My next thought was - what does Microsoft do in the framework classes? I poked around the Rotor source code site, trying to find an example. Guess what? Couldn't find one. The only protected members that I found were either internal or had no corresponding public property. The majority of the fields that I looked at were simply declared as private with a public accessor property.

Now, this was hardly a comprehensive search - I just picked several files at pseudo-random (trying to pick files that would have reasonably substantial code in them). I haven't downloaded the full Rotor source and searched. But that said, I thought it was kind of interesting that there wasn't a single example to be found. The style guide says that a protected instance field is "rarely used", and I get that. The only reason I can envision using one is in performance sensitive code where you don't want a deriving class to incur the overhead of a property accessor. Pretty obscure. So I'm going to stick with the private field, public/protected property model.

As a final note, in poking around the Rotor source it seems that the Microsoft developers mostly used three different coding styles for fields - camel casing, leading underscore, and the old Hungarian style "m_". I wonder if they've settled on one style going forward.

I also wonder if I should just get a life and stop spending so much time thinking (and blogging) about stuff like this.

Published Wednesday, August 13, 2003 2:00 PM by kevindente

Comments

# re: .NET naming conventions and protected members@ Wednesday, August 13, 2003 11:17 PM

When we were doing our coding standards for C# we came across this problem. We had a bunch of internal MS people pinged to see what they did. The result was there was no standard, apart from just using case in-sensitive fields.

We decided against that in-case we had some VB.Net development.

We now use m_, which works OK, but is not ideal.

Blair Stephenson

# re: .NET naming conventions and protected members@ Thursday, August 14, 2003 2:52 AM

Using a prefix (e.g. "m_") has its advantages when using intellisense, as shown here: http://weblogs.asp.net/rweigelt/posts/21741.aspx

(Whether to use full, i.e. type-dependent, hungarian notation is a matter of choice and also depends on what the majority of a development team decides on.)

Roland Weigelt

# re: .NET naming conventions and protected members@ Saturday, August 16, 2003 2:19 PM

Well, not excatly what you are looking for, but one fix here is to not have any public or protected fields. There is just no reason to expose those. You can always use a property to abstract them. That will save your butt some day when you change some implementation details without breaking client code.

Brad Abrams

# re: .NET naming conventions and protected members@ Saturday, September 20, 2003 10:40 AM

A clarification on what Brad said: you use protected properties, instead of fields. This is the standard in .net and I it is in the guidelines as well.

As he said, you should not allow the public to ever see a field. It just causes problems.

Frank Hileman

# re: .NET naming conventions and protected members@ Wednesday, June 30, 2004 3:51 PM

Assuming you have valid reasons for needing protected fields, there actually are two CLS-compliance issues which led us to settle on using m_:

1) We elminated using camel/pascal casing to differentiate protected fields and public properties since protected fields and public properties of the same name, varying only by case, are not CLS compliant. Therefore, if you use pascal casing for a property (e.g. Foo) and camel casing for the protected field (e.g. foo), you will not be CLS-complaint, and therefore your component can not be used by some CLS-compliant languages (such as Visual Basic .NET in this case).

2) We eliminated preceeding fields with an underscore, since protected fields with an underscore (e.g. _foo) are also not CLS-complaint, thus may be inaccessible by some CLS-compliant languages (although happens to work fine with C# and VB .NET)

Therefore, we settled on m_ for fields, since this works for protected fields as well, assuming you have protected fields.

RC

# Naming Conventions, CLSCompliantAttribute, Underscores, etc@ Saturday, February 19, 2005 3:18 PM

TrackBack

# Naming Conventions, CLSCompliantAttribute, Underscores, etc@ Saturday, February 19, 2005 3:23 PM

TrackBack

Leave a Comment

(required) 
(required) 
(optional)
(required)