• Scorekeeper (unregistered)
    give yourself a point!

    Yeah, give yourself a point.... of DUMB!

    Bahahaha

  • Jacob (unregistered) in reply to TheCPUWizard
    TheCPUWizard:
    Actually C# allows you to switch on anything that is equatible (e.g. strings). For non-integral (or low density integral) this gets implemented as a chain of is/else if/else if/ else. Many developers find the swtich syntax easier to follow than having to match up the if statements.

    Awesome! Polymorphism is for fags anyway. I hear Java 7 is getting a similar bastard of a way to avoid writing OO code

  • Fyodor Soikin (unregistered) in reply to Anonymous User
    Is "bool" an object in C#? * No, it's a value type.

    Is "Bool" an object in C#?

    • Yes. In fact, I believe it's a 'Boxed' format of bool.

    This is complete bullshit. "bool" and "System.Bool" are absolute synonyms, no difference whatsoever. C# is not Java.

  • Jacob (unregistered) in reply to Koppernicus
    Koppernicus:
    I for one am glad that the original programmer decided to future proof his code, so that when the new Tri-State boolean is introduced in C# 13.0, his code will detect the change and blow up instead of failing silently.
    public static void main(String[] args) {
    		Boolean b = null;
    		bool(b);
    	}
    	
    	static void bool(boolean b) {
    		System.err.println(b);
    	}
    
  • Anonymous (unregistered) in reply to frits
    frits:
    Anonymous User:
    Is "bool" an object in C#? * No, it's a value type.

    Is "Bool" an object in C#?

    • Yes. In fact, I believe it's a 'Boxed' format of bool.

    The main difference between the two is how the data is stored; objects are generally 'like' C++ pointers (with a lot more guards) while value types are generally direct bits of memory. So values can't be 'null', while objects can. This also explains why a C# struct can't be null; it's considered a value-type, if a rather complex one.

    Also, Enums in C# are usually value-types (int by default, unless you override), so they shouldn't be nullable. So, to represent a base/unknown/default state that doesn't match a 'good' state, you would need either another value that you'd always have to check, or initial state of base/unknown/default to do the same thing.

    Bonus points: 'Enum' is its own class type with its own static functions that work nicely with enum types. Take a peek at it, see if any of them might be useful for what you're doing.

    Try again. ValueType derives from Object.

    Spot on. Object is the root of the type hierarchy, end of story. EVERYTHING derives from Object, including value types (as frits has shown above).

  • (cs)

    I don't understand what files have to do with boolean logic. Is it a database thing?

    I can understand adding "MAYBE" to TRUE and FALSE. But FILE_NOT_FOUND?

    Good Lord, people, do you think we can eventually do all of our serious programming via email?

  • (cs) in reply to enim
    enim:
    Serious question: is bool an object in C#?

    Technically, yes. bool is an alias for the System.Boolean struct.

    However, because it's a struct, it's allocated on the stack rather than the heap.

    There's also a tri-state (nullable) version bool? which is a shortcut for System.Nullable<System.Boolean>.

  • pengi (unregistered) in reply to The Nerve

    That depends. If True = 0, False = 1, that would be reversed.

    And what happens with FileNotFound = 2 then?

  • (cs) in reply to Anonymous User
    Anonymous User:
    Is "bool" an object in C#? * No, it's a value type.

    Is "Bool" an object in C#?

    • Yes. In fact, I believe it's a 'Boxed' format of bool.

    The main difference between the two is how the data is stored; objects are generally 'like' C++ pointers (with a lot more guards) while value types are generally direct bits of memory. So values can't be 'null', while objects can. This also explains why a C# struct can't be null; it's considered a value-type, if a rather complex one.

    In Java, yes. In C#, no. In C#, everything, including value types (structs), are objects. The C# 'bool' keyword is just syntactic sugar for the .NET 'System.Boolean' struct. It makes no difference whatsoever whether you use 'bool' or 'Boolean.'

  • Jacob (unregistered) in reply to mott555
    mott555:
    Anonymous User:
    Is "bool" an object in C#? * No, it's a value type.

    Is "Bool" an object in C#?

    • Yes. In fact, I believe it's a 'Boxed' format of bool.

    The main difference between the two is how the data is stored; objects are generally 'like' C++ pointers (with a lot more guards) while value types are generally direct bits of memory. So values can't be 'null', while objects can. This also explains why a C# struct can't be null; it's considered a value-type, if a rather complex one.

    In Java, yes. In C#, no. In C#, everything, including value types (structs), are objects. The C# 'bool' keyword is just syntactic sugar for the .NET 'System.Boolean' struct. It makes no difference whatsoever whether you use 'bool' or 'Boolean.'

    Why do both exist, then?

  • Jesse (unregistered) in reply to Anonymous

    As long as we're being pedantic: no, not everything. Pointer types (used in unsafe mode) do not derive from System.Object.

  • (cs) in reply to Jacob
    Jacob:
    Why do both exist, then?
    To make you type less. Its just a shortcut. There are however value types and reference types. Both ARE objects. as was mentioned earlier the difference is in how the types are allocated.
  • Jesse (unregistered) in reply to Jacob

    System.Bool is the fully qualified .NET type name: Bool is defined in the System namespace in the standard library. "bool" is a C# language keyword defined as a shortcut, the same way "int" is a shortcut for System.Int32 and so on. In practice, the only value is that it saves you from having to hit shift as often.

  • Anonymous Coward (unregistered) in reply to frits
    frits:
    enim:
    Serious question: is bool an object in C#?

    Now I have to hope and prey this comment doesn't get deleted by the over-zealous moderators. I wonder at the motivation behind the recent police effort.

    Serious question: Does the internet not work for you? This question can be answered doing a minimal amount of research. Alternatively, you could troll over at codeproject.com/stackoverflow.com to get the answer.

    You fell for the troll. He did warn you he was preying.

  • Anonymous (unregistered) in reply to Jacob
    Jacob:
    mott555:
    Anonymous User:
    Is "bool" an object in C#? * No, it's a value type.

    Is "Bool" an object in C#?

    • Yes. In fact, I believe it's a 'Boxed' format of bool.

    The main difference between the two is how the data is stored; objects are generally 'like' C++ pointers (with a lot more guards) while value types are generally direct bits of memory. So values can't be 'null', while objects can. This also explains why a C# struct can't be null; it's considered a value-type, if a rather complex one.

    In Java, yes. In C#, no. In C#, everything, including value types (structs), are objects. The C# 'bool' keyword is just syntactic sugar for the .NET 'System.Boolean' struct. It makes no difference whatsoever whether you use 'bool' or 'Boolean.'

    Why do both exist, then?

    He just told you, it's syntactic sugar. Some people feel more comfortable using a keyword such as 'bool' instead of a type reference such as 'System.Boolean'. But it's just syntactic sugar, a facade over the real type.

  • (cs) in reply to frits
    frits:
    C# fun facts: Boolean keywords are all lowercase. ToString() and TrueString/FalseString are capitalized. Parse() expects capitalization.

    This method will not return "true" or "false" as is.

    So, not a real WTF, he just left off a little bit...

    public static string Bool2Str(bool b)

    {

    switch(b)
    
    {
    
        case true:
    
            return System.Boolean.TrueString.ToLower();
    
        case false:
    
            return System.Boolean.FalseString.ToLower();
    
        default:
    
            return "error";
    
    }
    

    }

    (drip, drip, drip...)

    Addendum (2010-09-08 11:49):

    frits:
    C# fun facts: Boolean keywords are all lowercase. ToString() and TrueString/FalseString are capitalized. Parse() expects capitalization.

    This method will not return "true" or "false" as is.

    So, not a real WTF, he just left off a little bit...

    public static string Bool2Str(bool b)
    
    {
        switch(b)
        {
            case true:
                return System.Boolean.TrueString.ToLower();
            case false:
                return System.Boolean.FalseString.ToLower();
            default:
                return "error";
        }
    }
    

    (drip, drip, drip...)

    Addendum (2010-09-08 11:50): Dang, ran out of time.

  • Bob (unregistered)

    Like my old mentor used to say,

    The way it works is not democratic. It will either do one thing or another, and it has nothing to do with how people think it ought to work.

    That being said, it seems that people here have no clue, but there's a whole lot of "voting" going on.

  • Ken B. (unregistered)

    Not very enterprisey. This is The Right Way(tm) to do it:

    SELECT StringValue FROM BoolToString WHERE BoolValue = %1;

  • Paula (unregistered)

    C'mon Darth!

    All this nitpicking is boring. How about something along the lines of:

    "I have C pounded your Bool2Str. Pray that I don't C pound it further."

  • ShatteredArm (unregistered) in reply to John Preston
    John Preston:
    Actually, all .Net languages allow Booleans (and other data types) to have a 3rd (or n+1th) state. Not set. So you may not be able to get FileNotFound, but Not Set is pretty close.

    That leaves you with a Boolean with a 3rd state. So really, more of a Trilean than a Boolean.

    Fun fact, you can never set a variable back to Not Set once it has a value.

    FALSE.

    All value types have their values automatically set to their default value when the stack space is allocated. That default value is whatever the equivalent of 0 is. For bool that is false.

    Take the following code: static bool b;

        static void Main(string[] args)
        {
    
            Console.Out.Write(b); //prints "False"
    
        }
    

    That is the whole purpose of Nullable<>.

  • B (unregistered) in reply to pitchingchris
    pitchingchris:
    Hypersw:
    Well, by lack of expertise you might consider that this function is actually useless.

    However, it is quite possible to make it yield "error" if you pass in a boolean value converted from an integer 2 (or any other integer except zero or one).

    Or did you think boolean is just about "false" or "true" constants? It's an integer after all, just with special semantic.

    A working sample:

    private static unsafe void Main() {   bool val;   ((byte)&val) = 2;   Console.WriteLine(Bool2Str(val)); }

    public static string Bool2Str(bool b) {   switch (b)   {     case true:       return System.Boolean.TrueString;     case false:       return System.Boolean.FalseString;     default:       return "error";   } }

    Of course it's not reasonable in pure C#, but should you be interoperating with some legacy C++ components...

    I don't think this is a good test either. bool will evaluate to either true or false, so even if you didn't marshal a win32 BOOL or some other value correctly, it simply will take the bit value at a location. There is no 'third' or other state.

    More than likely, if there is an error, it will evaluate to true, since anything other than 0 will convert to true.

    This actually does print "error". Surprised the hell out of me too.

  • (cs) in reply to Jacob
    Jacob:
    TheCPUWizard:
    Actually C# allows you to switch on anything that is equatible (e.g. strings). For non-integral (or low density integral) this gets implemented as a chain of is/else if/else if/ else. Many developers find the swtich syntax easier to follow than having to match up the if statements.

    Awesome! Polymorphism is for fags anyway. I hear Java 7 is getting a similar bastard of a way to avoid writing OO code

    switch..case is a useful construct even in OO to handle state. Objects can change their state but cannot change their type.

    You could handle a change of state by changing some object it holds to a different one that derives from a base-class somewhere and call its virtual methods. Of course that means you need to know which class to create for the new state, which means you may well end up using switch..case anyway to decide, particularly if you are going to load it lazily.

    Presumably if we don't allow switch..case we also should not allow if statements, as it should hold one class to implement the true case and one to implement the false case. (I have actually refactored classes that were doing a lot of that on the same boolean all over the place to use one of two implementation classes. This was a set-once-only-on-construction boolean as it happens so it clearly shouted out to me to do it that way).

  • Escogido (unregistered)

    Well it could have been worse...

    if (b.ToString().Length == 5)... // checks for false

  • Matt Westwood (unregistered) in reply to SilentRunner
    SilentRunner:
    I don't understand what files have to do with boolean logic. Is it a database thing?

    I can understand adding "MAYBE" to TRUE and FALSE. But FILE_NOT_FOUND?

    Good Lord, people, do you think we can eventually do all of our serious programming via email?

    In case you're new here and seriously bewildered, FILE_NOT_FOUND is an elderly in-joke from a WTF of days of yore where someone genuinely implemented a 3-state boolean as TRUE, FALSE and FILE_NOT_FOUND. It entered WTF folklore. No doubt some more dedicated WTFers can find the precise reference. It would be brillant if they can.

  • (cs) in reply to Steenbergh
    Steenbergh:
    Aargh! I HATE conversion functions with a literal '2' between the one datatype and the other. It just seems so unprofessional.
    And what about when they implement Boolean.Maybe? Boolean.Sometimes? Boolean.NotToday? ...
  • Dudefella (unregistered) in reply to Keith Williams
    ///
    ///Do not use.  Kept only in case [Programmer's name here] was dumb enough to use reflection
    ///
    [Obsolete("Do not use.  Kept only in case [Programmer's name here] was dumb enough to use reflection")]
    public static string Bool2Str(bool b)
    {
       return b.ToString();
    }

    FTFY.

  • Anonymous (unregistered) in reply to Matt Westwood
    Matt Westwood:
    SilentRunner:
    I don't understand what files have to do with boolean logic. Is it a database thing?

    I can understand adding "MAYBE" to TRUE and FALSE. But FILE_NOT_FOUND?

    Good Lord, people, do you think we can eventually do all of our serious programming via email?

    In case you're new here and seriously bewildered, FILE_NOT_FOUND is an elderly in-joke from a WTF of days of yore where someone genuinely implemented a 3-state boolean as TRUE, FALSE and FILE_NOT_FOUND. It entered WTF folklore. No doubt some more dedicated WTFers can find the precise reference. It would be brillant if they can.

    That won't be a problem, all the regulars remember this short-but-sweet classic: http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx

    It was linkified but, obviously, Akismet refused it as spam. Fu*k you Akismet.

  • (cs) in reply to galgorah
    galgorah:
    I present this abomination. Feel free to modify and add even more WTF to the below code.

    public interface IBoolVal { String StringRep(); }

    public class FalseBool : IBoolVal
    {
        string IBoolVal.StringRep()
        {
            return "False";
        }
    }
    
    public class TrueBool : IBoolVal
    {
        string IBoolVal.StringRep()
        {
            return "True";
        }
    }
    
    public class BoolHelper : System.Object
    {
        public static bool ConvertStringToBool(string value)
        {
            string sTrue = ConvertBoolToString(true).ToLower();
            string sFalse = ConvertBoolToString(false).ToLower();
    
            if (sTrue == value.ToLower())
                return true;
            else 
                return false;
        }
        public static string ConvertBoolToString(bool value)
        {
            IBoolVal v;
            if (value) v = new TrueBool(); else  v = new FalseBool();
            return v.StringRep();
        }
    }</div></BLOCKQUOTE>
    

    I showed this around the shop. First programmer looked at it for a few minutes. He started rambling on about how he could abstract out the method names and call them dynamically or something. I think he mentioned XML at one point. Second programmer had his head pretty deep in some code he was writing, couldn't be bothered. Third programmer shook his head, read the code again, shook his head again, then wandered off muttering something I couldn't hear, stopping every few steps to look upwards and mutter more, all the way back to his desk.

    Showed it to a project manager. He said its fine as long as it doesn't cause any deadlines to slip. Why do they all use the word 'slip' like that anyway?

    Showed it to the architect. He asked me something about how gracefully it will fail if the database connection is severed.

    Lastly, showed it to a business manager. He asked me what it does. After I explained it to him, his eyes lit up, and hurried off to his desk to write up something to send to clients about some planned upgrade to the system, and to figure how much extra we can charge for it.

  • (cs) in reply to Anonymous Coward
    Anonymous Coward:
    frits:
    enim:
    Serious question: is bool an object in C#?

    Now I have to hope and prey this comment doesn't get deleted by the over-zealous moderators. I wonder at the motivation behind the recent police effort.

    Serious question: Does the internet not work for you? This question can be answered doing a minimal amount of research. Alternatively, you could troll over at codeproject.com/stackoverflow.com to get the answer.

    You fell for the troll. He did warn you he was preying.

    To be fair, I did give him some new leads.

  • m (unregistered) in reply to mott555
    I like to put a default block in switch statements that handle enums that throws an exception [...]. That way, if in the future someone adds values to the enum but doesn't update the other code, it'll throw an exception and hopefully it'll get noticed during testing.

    which actually could be (or, with a sane language, will be (IS8652 5.4§9)) detected reliably during compilation iff the default case is not present. (gcc does this with -Wswitch)

    captcha enim: mistyped enum?

  • boog (unregistered) in reply to Matt Westwood
    Matt Westwood:
    SilentRunner:
    I don't understand what files have to do with boolean logic. Is it a database thing?

    I can understand adding "MAYBE" to TRUE and FALSE. But FILE_NOT_FOUND?

    Good Lord, people, do you think we can eventually do all of our serious programming via email?

    In case you're new here and seriously bewildered, FILE_NOT_FOUND is an elderly in-joke from a WTF of days of yore where someone genuinely implemented a 3-state boolean as TRUE, FALSE and FILE_NOT_FOUND. It entered WTF folklore. No doubt some more dedicated WTFers can find the precise reference. It would be brillant if they can.

    Shouldn't be hard, since today's article links directly to it in the first paragraph.

  • uuang (unregistered) in reply to verto
    verto:
    I'm glad Alex reminded us of that previous article with FILE_NOT_FOUND. I had almost forgotten about it since yesterday.

    +1

  • The Bytemaster (unregistered) in reply to Squeeself
    Squeeself:
    String.IsNullOrEmpty() http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx

    This is but one of the many reasons I <3 C#.

    In .Net 4.0 (C# 4.0, and all other languages) we now have String.IsNullOrWhiteSpace() as well. Does the String.IsNullOrEmpty check, and then for being only whitespace.

  • uuang (unregistered) in reply to somedude
    somedude:
    galgorah:
    I present this abomination. Feel free to modify and add even more WTF to the below code.

    public interface IBoolVal { String StringRep(); }

    public class FalseBool : IBoolVal
    {
        string IBoolVal.StringRep()
        {
            return "False";
        }
    }
    
    public class TrueBool : IBoolVal
    {
        string IBoolVal.StringRep()
        {
            return "True";
        }
    }
    
    public class BoolHelper : System.Object
    {
        public static bool ConvertStringToBool(string value)
        {
            string sTrue = ConvertBoolToString(true).ToLower();
            string sFalse = ConvertBoolToString(false).ToLower();
    
            if (sTrue == value.ToLower())
                return true;
            else 
                return false;
        }
        public static string ConvertBoolToString(bool value)
        {
            IBoolVal v;
            if (value) v = new TrueBool(); else  v = new FalseBool();
            return v.StringRep();
        }
    }</div></BLOCKQUOTE>
    

    I showed this around the shop. First programmer looked at it for a few minutes. He started rambling on about how he could abstract out the method names and call them dynamically or something. I think he mentioned XML at one point. Second programmer had his head pretty deep in some code he was writing, couldn't be bothered. Third programmer shook his head, read the code again, shook his head again, then wandered off muttering something I couldn't hear, stopping every few steps to look upwards and mutter more, all the way back to his desk.

    Showed it to a project manager. He said its fine as long as it doesn't cause any deadlines to slip. Why do they all use the word 'slip' like that anyway?

    Showed it to the architect. He asked me something about how gracefully it will fail if the database connection is severed.

    Lastly, showed it to a business manager. He asked me what it does. After I explained it to him, his eyes lit up, and hurried off to his desk to write up something to send to clients about some planned upgrade to the system, and to figure how much extra we can charge for it.

    I'd love to have that kinda free time.

  • Peter (unregistered) in reply to Corey
    Corey:
    I think that was a preemptive FILE_NOT_FOUND. You know, to keep the "what about FILE_NOT_FOUND dumb-asses quiet."
    It's good to remember to close your quotation marks. It's better to remember to close them in the right place.
  • (cs) in reply to Anonymous
    Anonymous:
    frits:
    Anonymous User:
    Is "bool" an object in C#? * No, it's a value type.

    Is "Bool" an object in C#?

    • Yes. In fact, I believe it's a 'Boxed' format of bool.

    The main difference between the two is how the data is stored; objects are generally 'like' C++ pointers (with a lot more guards) while value types are generally direct bits of memory. So values can't be 'null', while objects can. This also explains why a C# struct can't be null; it's considered a value-type, if a rather complex one.

    Also, Enums in C# are usually value-types (int by default, unless you override), so they shouldn't be nullable. So, to represent a base/unknown/default state that doesn't match a 'good' state, you would need either another value that you'd always have to check, or initial state of base/unknown/default to do the same thing.

    Bonus points: 'Enum' is its own class type with its own static functions that work nicely with enum types. Take a peek at it, see if any of them might be useful for what you're doing.

    Try again. ValueType derives from Object.

    Spot on. Object is the root of the type hierarchy, end of story. EVERYTHING derives from Object, including value types (as frits has shown above).

    Then why does the term "boxing" exist? If the primitive int "4" is already an object, why does the system need to box it in an instance of System.Int32 in order to call ToString() on it? C# isn't Smalltalk.

  • (cs) in reply to Jaime
    Jaime:
    Then why does the term "boxing" exist? If the primitive int "4" is already an object, why does the system need to box it in an instance of System.Int32 in order to call ToString() on it? C# isn't Smalltalk.

    Boxing and unboxing in .NET don't quite mean the same thing as in Java. In .NET, boxing is just a way of storing a value type on the heap.

    http://msdn.microsoft.com/en-us/library/yz2be5wk%28VS.80%29.aspx

  • hhhhhhhhhhhhhhhhharry (unregistered) in reply to Jaime
    Jaime:
    Anonymous:
    frits:
    Anonymous User:
    Is "bool" an object in C#? * No, it's a value type.

    Is "Bool" an object in C#?

    • Yes. In fact, I believe it's a 'Boxed' format of bool.

    The main difference between the two is how the data is stored; objects are generally 'like' C++ pointers (with a lot more guards) while value types are generally direct bits of memory. So values can't be 'null', while objects can. This also explains why a C# struct can't be null; it's considered a value-type, if a rather complex one.

    Also, Enums in C# are usually value-types (int by default, unless you override), so they shouldn't be nullable. So, to represent a base/unknown/default state that doesn't match a 'good' state, you would need either another value that you'd always have to check, or initial state of base/unknown/default to do the same thing.

    Bonus points: 'Enum' is its own class type with its own static functions that work nicely with enum types. Take a peek at it, see if any of them might be useful for what you're doing.

    Try again. ValueType derives from Object.

    Spot on. Object is the root of the type hierarchy, end of story. EVERYTHING derives from Object, including value types (as frits has shown above).

    Then why does the term "boxing" exist? If the primitive int "4" is already an object, why does the system need to box it in an instance of System.Int32 in order to call ToString() on it? C# isn't Smalltalk.

    i don't think it needs to box it to call .ToString(), C# isn't java either. as stated before System.Int32 == int (C#) == Integer (VB). It's not a different type, even if it does highlight the type name a different colour in VS

    Boxing in C# is object o = 12;

    and just stores the value in a different place (heap instead of stack)

    any further opinions?

  • Paster (unregistered) in reply to The Nerve
    The Nerve:
    Fixed?
    // "true" or "false"
    public static string Bool2Str(bool b)
    {
        return b ? System.Boolean.TrueString : System.Boolean.FalseString;
    }
    

    Oh no, no no.

    // "True", "False" or "error"
    public static string Bool2Str(bool b)
    {
        return b ? System.Boolean.TrueString : (!b ? System.Boolean.FalseString : "error");
    }
    
  • (cs) in reply to hhhhhhhhhhhhhhhhharry
    hhhhhhhhhhhhhhhhharry:
    Jaime:
    Anonymous:
    frits:
    Anonymous User:
    Is "bool" an object in C#? * No, it's a value type.

    Is "Bool" an object in C#?

    • Yes. In fact, I believe it's a 'Boxed' format of bool.

    The main difference between the two is how the data is stored; objects are generally 'like' C++ pointers (with a lot more guards) while value types are generally direct bits of memory. So values can't be 'null', while objects can. This also explains why a C# struct can't be null; it's considered a value-type, if a rather complex one.

    Also, Enums in C# are usually value-types (int by default, unless you override), so they shouldn't be nullable. So, to represent a base/unknown/default state that doesn't match a 'good' state, you would need either another value that you'd always have to check, or initial state of base/unknown/default to do the same thing.

    Bonus points: 'Enum' is its own class type with its own static functions that work nicely with enum types. Take a peek at it, see if any of them might be useful for what you're doing.

    Try again. ValueType derives from Object.

    Spot on. Object is the root of the type hierarchy, end of story. EVERYTHING derives from Object, including value types (as frits has shown above).

    Then why does the term "boxing" exist? If the primitive int "4" is already an object, why does the system need to box it in an instance of System.Int32 in order to call ToString() on it? C# isn't Smalltalk.

    i don't think it needs to box it to call .ToString(), C# isn't java either. as stated before System.Int32 == int (C#) == Integer (VB). It's not a different type, even if it does highlight the type name a different colour in VS

    Boxing in C# is object o = 12;

    and just stores the value in a different place (heap instead of stack)

    any further opinions?

    OK, bad example. Let me try again...

    Then why does the term "boxing" exist? If the primitive int "4" is already an object, why does the system need to box it in order to assign it to a variable of type Object? C# isn't Smalltalk.

    This is fundamentally a philisophical discussion. It boils down to "Is something that is converted to an object whenever it needs to behave like one really an object?" From an interface perspective, it's an object. From an implementation perspective, it's not an object.

  • TN (unregistered)

    How about localization?

    If you want to show the true/false status on the screen in Hindi, Russian and Guyanese?

    Hmm? How about that, smarty pants!

  • opto (unregistered)

    Its sometimes useful to convert a boolean to a string that is more readable than true/false, depending on the context, such as: yes/no, confirm/deny, fail/win, etc.

    But this implementation isn't very good.

  • BTR (unregistered) in reply to TN
    TN:
    How about localization? Dear TN, If you want to show the true/false status on the screen in Hindi, Russian and Guyanese? In case you can't tell, this is a grow-up place. The fact that you insist on your ridiculous localization clearly shows you're to young and too stupid to be using pants. Hmm? How about that, smarty pants!
    Go away and grow up. I've always wondered: are the English ticked off when they have to go to wikipedia.org as opposed to wikipaedia.org? I think I would be, if the tables were turned. Sincerely, Bert Glanstron
  • LB (unregistered) in reply to TN
    TN:
    How about localization?

    If you want to show the true/false status on the screen in Hindi, Russian and Guyanese?

    Hmm? How about that, smarty pants!

    How odd. Not having worked in C# before, I assumed on seeing this that that's precisely what System.Boolean.TrueString and System.Boolean.FalseString would be for — to provide the system's localized equivalents of the literals "True" and "False". I just did a bit of browsing about it and that doesn't seem to be the case, though. System.Boolean.TrueString really is just the literal string "True" and System.Boolean.FalseString the literal string "False". That seems to leave these fields pretty useless. If all they provide is a literal, then you can just use the corresponding literal instead.

  • Andrew (unregistered) in reply to Jacob

    It's been there for years: java.lang.reflect

  • MWF (unregistered) in reply to LB
    LB:
    TN:
    How about localization?

    If you want to show the true/false status on the screen in Hindi, Russian and Guyanese?

    Hmm? How about that, smarty pants!

    How odd. Not having worked in C# before, I assumed on seeing this that that's precisely what System.Boolean.TrueString and System.Boolean.FalseString would be for — to provide the system's localized equivalents of the literals "True" and "False". I just did a bit of browsing about it and that doesn't seem to be the case, though. System.Boolean.TrueString really is just the literal string "True" and System.Boolean.FalseString the literal string "False". That seems to leave these fields pretty useless. If all they provide is a literal, then you can just use the corresponding literal instead.

    Yep, System.Boolean does not "properly" implement ToString(IFormatProvider):

    http://msdn.microsoft.com/en-us/library/s802ct92.aspx

    Other types use this overload in order to provide culture-specific string representations.

  • Keith Williams (unregistered) in reply to John Preston

    They are called nullable types, and they only work that way if they are declared as nullable. A regular System.Boolean cannot hold a third value.

  • (cs) in reply to LB
    LB:
    System.Boolean.TrueString really is just the literal string "True" and System.Boolean.FalseString the literal string "False". That seems to leave these fields pretty useless. If all they provide is a literal, then you can just use the corresponding literal instead.

    One advantage to doing it this way is avoiding the creation of a new object for the string literal every time it's used. C# may have some means of doing that behind the scenes for commonly-used literals, but this way makes it certain that only those two will ever be created if the built-in ToString logic is used.

  • Anonymous (unregistered) in reply to Jaime
    Jaime:
    Then why does the term "boxing" exist? If the primitive int "4" is already an object, why does the system need to box it in an instance of System.Int32 in order to call ToString() on it? C# isn't Smalltalk.

    Objects in C# come in two broad flavours - Reference Types and Value Types. An Object is a reference type unless it inherits from System.ValueType.

    Boxing is used to store a subclass of ValueType in a variable expecting an Reference Type. Of course, there's only one situation this can happen - because ValueType has only one reference-type ancestor, the only time this happens is when you store a value-type in a variable of Object. (Any other reference type is a compile time error.)

    The upshot of all this was in .Net 1.0 and 1.1, you could put an 'int' (System.Int32) into an ArrayList, which was a list of Object. The runtime would (unlike Java) automatically box the primitive 'int' into a reference type so it could be treated by the ArrayList just like any other reference-type subclass of Object.

    And I don't believe that the runtime does box an int to call ToString on it because System.Int32 would directly implement ToString(..).

    So, int IS an object. The difference is that int is not a "Reference Type".

    (These days, generics generally remove the need for boxing - a System.Collections.Generic.List(Of Integer) does not have the boxing/unboxing performance overhead.)

  • PRMan (unregistered) in reply to Jellineck
    Jellineck:
    Larry:
    TRWTF is that C-Pound allows you to use a bool in a switch statement.

    I can't wait for someone to call C# 'C-Pound' in an interview.

    You mean the interviewer? That's already happened to me.

Leave a comment on “Truthful Strings”

Log In or post as a guest

Replying to comment #:

« Return to Article