- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Yeah, give yourself a point.... of DUMB!
Bahahaha
Admin
Awesome! Polymorphism is for fags anyway. I hear Java 7 is getting a similar bastard of a way to avoid writing OO code
Admin
This is complete bullshit. "bool" and "System.Bool" are absolute synonyms, no difference whatsoever. C# is not Java.
Admin
Admin
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).
Admin
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?
Admin
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>.
Admin
That depends. If True = 0, False = 1, that would be reversed.
And what happens with FileNotFound = 2 then?
Admin
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.'
Admin
Why do both exist, then?
Admin
As long as we're being pedantic: no, not everything. Pointer types (used in unsafe mode) do not derive from System.Object.
Admin
Admin
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.
Admin
You fell for the troll. He did warn you he was preying.
Admin
Admin
So, not a real WTF, he just left off a little bit...
public static string Bool2Str(bool b)
{
}
(drip, drip, drip...)
Addendum (2010-09-08 11:49):
So, not a real WTF, he just left off a little bit...
(drip, drip, drip...)
Addendum (2010-09-08 11:50): Dang, ran out of time.
Admin
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.
Admin
Not very enterprisey. This is The Right Way(tm) to do it:
Admin
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."
Admin
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;
That is the whole purpose of Nullable<>.
Admin
This actually does print "error". Surprised the hell out of me too.
Admin
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).
Admin
Well it could have been worse...
if (b.ToString().Length == 5)... // checks for false
Admin
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.
Admin
Admin
FTFY.
Admin
It was linkified but, obviously, Akismet refused it as spam. Fu*k you Akismet.
Admin
Admin
To be fair, I did give him some new leads.
Admin
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?
Admin
Admin
+1
Admin
Admin
Admin
Admin
Admin
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
Admin
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?
Admin
Oh no, no no.
Admin
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.
Admin
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!
Admin
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.
Admin
Admin
Admin
It's been there for years: java.lang.reflect
Admin
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.
Admin
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.
Admin
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.
Admin
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.)
Admin
You mean the interviewer? That's already happened to me.