Enumeration Format Strings. Difference between g and f?

  • Thread starter Thread starter John Bentley
  • Start date Start date
John,
Can someone give me an example where an enumeration format string of g would
return a different result to that of f?

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 wrote (at this indent level):
-------------------------------------------------

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.

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.
 
Back
Top