Connecting Tech Pros Worldwide Forums | Help | Site Map

Enumeration Format Strings. Difference between g and f?

John Bentley
Guest
 
Posts: n/a
#1: Jul 21 '05
Can someone give me an example where an enumeration format string of g would
return a different result to that of f?

http://msdn.microsoft.com/library/de...pguide/html/cp
conenumerationformatstrings.asp

--




Mattias Sjögren
Guest
 
Posts: n/a
#2: Jul 21 '05

re: Enumeration Format Strings. Difference between g and f?


John,
[color=blue]
>Can someone give me an example where an enumeration format string of g would
>return a different result to that of f?[/color]

enum E
{
A = 0x1,
B = 0x2,
}

E e3 = (E)3;
Console.WriteLine( e3.ToString( "g" ) );
Console.WriteLine( e3.ToString( "f" ) );

"g" will print 3, "f" will print A, B. The f specifier gives you flags
formatting, even if the Flags attribute hasn't been applied to the
enum type.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
John Bentley
Guest
 
Posts: n/a
#3: Jul 21 '05

re: Enumeration Format Strings. Difference between g and f?


> -------------------------------------------------[color=blue]
> Mattias Sjögren wrote (at this indent level):
> -------------------------------------------------[/color]
[color=blue][color=green]
>> Can someone give me an example where an enumeration format string of
>> g would return a different result to that of f?[/color]
>
> enum E
> {
> A = 0x1,
> B = 0x2,
> }
>
> E e3 = (E)3;
> Console.WriteLine( e3.ToString( "g" ) );
> Console.WriteLine( e3.ToString( "f" ) );
>
> "g" will print 3, "f" will print A, B. The f specifier gives you flags
> formatting, even if the Flags attribute hasn't been applied to the
> enum type.[/color]

Thanks Mattias. I can now see the difference. Interestingly if you change

E e3 = (E)3; //to
E e3 = (E)1;

You get A for both cases.



Closed Thread