Interesting .NET readonly behavior

So I will start out by saying that this is far from a new thing, but I found this out recently and thought I would share this little tidbit of information regarding the readonly keyword in .NET.

As I am sure many of you if not all reading this article know, the readonly keyword is primarily used make fields within a class (often an abstract base class) readonly outside of that particular class. That makes sense right? Ok, now the interesting part comes if and when you want to do this with some kind of array ( List,Collections,Dictionaries etc..). Apparently doing something like this does not work as expected.

protected readonly List<string> htmlAttributes { get; private set; }

If you access this from another class, (assuming you did initialize it in the base class) the compiler will happily allow you to access this field and do an Add without a problem. This was a little confusing at first and I still unsure as to why this occurs, but there is a way to do what we are trying to do. Actually, there are two ways.

First way is rather simple, but just kinda feels I dunno.. icky… You can make a public property with a private setter

public List<string> htmlAttributes { get; private set; }

This works just fine and will get you what you want. The other way is a collection object called ReadOnlyCollection.

This has been around since 2.0, so for just about everyone, this will work just fine. to use this, see below:

private List<string> htmlAttributes = new List<string>();

public ReadOnlyCollection<string> HtmlAttributes
        get { return htmlAttributes.AsReadOnly(); }
Ok.. now we all can go on happily on our day

Happy Coding Smile


About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, C#, Programming Paradims and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s