Moving Beyond Enumerations
page 1 of 5
Published: 30 Aug 2011
Abstract
Enumerations provide far more readable code than magic strings or magic numbers. However, often developers try to stretch enums beyond their intended use. This article describes how to appropriately extend enums, and how to move beyond enums when the needs of the system require it.
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30524/ 57

Introduction

Enumerations in C# and .NET are value types with a set of related named constants.  Using enumerations often results in more readable code, and can help to ensure developers choose appropriate values by limiting possible options to those defined in the enum.  However, when the things represented by the enum start to require additional behavior, it is often best to move beyond the enum and start using classes instead.


View Entire Article

User Comments

Title: Good Catch   
Name: Steve Smith
Date: 2011-08-30 7:56:15 PM
Comment:
@Ian,
Good catch. Yes, those fields should be readonly, of course.

Thanks!
Steve
Title: public static roles should be readonly   
Name: Ian Mercer
Date: 2011-08-30 5:01:05 PM
Comment:
Totally agree that enums should be replaced with classes as soon as they stop being enums. One improvement to your class would be to make all the values readonly, e.g.

public static readonly Author ...






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 1:18:33 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search