Skip to Main Content
C# Cookbook, 2nd Edition
book

C# Cookbook, 2nd Edition

by Jay Hilyard, Stephen Teilhet
January 2006
Intermediate to advanced content levelIntermediate to advanced
1184 pages
43h 23m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook, 2nd Edition
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
28
|
Chapter 1: Numbers and Enumerations
Combining elements of this enumeration is a simple matter of using the bitwise OR
operator (
|). For example:
Language lang = Language.CSharp | Language.VBNET;
Discussion
Adding the Flags attribute to an enumeration marks this enumeration as individual
bit flags that can be ORed together. Using an enumeration of flags is no different
than using a regular enumeration type. It should be noted that failing to mark an
enumeration with the
Flags attribute will not generate an exception or a compile-
time error, even if the enumeration values are used as bit flags.
The addition of the
Flags attribute provides you with two benefits. First, if the Flags
attribute is placed on an enumeration, the ToString and ToString("G") methods
return a string consisting of the name of the constant(s) separated by commas. Oth-
erwise, these two methods return the numeric representation of the enumeration
value. Note that the
ToString("F") method returns a string consisting of the name of
the constant(s) separated by commas, regardless of whether this enumeration is
marked with the
Flags attribute. For an indication of why this works in this manner,
see the
"F" formatting type in Table 1-2 in Recipe 1.16.
The second benefit is that when you examine the code and encounter an
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 0596100639Supplemental ContentCatalog PageErrata