DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. [Flags] on Enums Make ToString() Smart

[Flags] on Enums Make ToString() Smart

Anders Abel user avatar by
Anders Abel
·
Nov. 23, 12 · Interview
Like (0)
Save
Tweet
Share
4.75K Views

Join the DZone community and get the full member experience.

Join For Free

Putting the [Flags] attribute on an enum changes ToStrings behaviour. It generates a comma separated list.

I’ve used enums a lot. I’ve occasionally used the [Flags] attribute too. But I’ve never paid attention to how ToString() changes behaviour when the [Flags] attribute is added. Not until a couple of days ago, when I learnt something new (to me at least) and useful. The [Flags] attribute will make ToString() return a comma separated list of the flags set.

Let’s look take a small enum with screen colors as an example.

[Flags]
enum Colors
{
  Black = 0,
  Red = 1,
  Green = 2,
  Blue = 4,
  Yellow = Red | Green
}

Colours are created as combinations of red, green and blue:

var violet = Colors.Red | Colors.Blue;
var white = Colors.Red | Colors.Green | Colors.Blue;
var myYellow = Colors.Red | Colors.Green;
var myRed = Colors.Black | Colors.Red;

The value of white is 5 and the value of yellow is 3. Without the [Flags] attribute, ToString() would return the numerical value, because there is no Colors value mapped to 3 or 5. With the [Flags] attribute applied, ToString() returns a comma separated list instead:

violet.ToString(): Red, Blue
myYellow.ToString(): Yellow
white.ToString(): Yellow, Blue
myRed.ToString(): Red
Colors.Black.ToString(): Black

It tries it best to make an as short a list as possible. When the red and green flags are set, they are combined into yellow. The zero option (black) is only displayed if the value is indeed zero.

This is nothing complex, neither anything that’s new (even though I’ve somehow missed it). I’m just happy I learnt something new that might be useful.

Attribute (computing) Data Types

Published at DZone with permission of Anders Abel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Quest for REST
  • A Complete Guide to AngularJS Testing
  • How to Develop a Portrait Retouching Function
  • How To Use Terraform to Provision an AWS EC2 Instance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: