Enumeration Format Strings. Difference between g and f?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Bentley

    Enumeration Format Strings. Difference between g and f?

    Can someone give me an example where an enumeration format string of g would
    return a different result to that of f?


    conenumerationf ormatstrings.as p

    --



  • Mattias Sjögren

    #2
    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.WriteLi ne( e3.ToString( "g" ) );
    Console.WriteLi ne( 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

    Please reply only to the newsgroup.

    Comment

    • John Bentley

      #3
      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.WriteLi ne( e3.ToString( "g" ) );
      > Console.WriteLi ne( 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.



      Comment

      Working...