|
I'm going to be pedantic here and state that the do something condition will never be executed in this example. BTW - to answer your question, the compiler recognises that if (a == true) is functionally equivalent to if (a) because it is clever enough to know that all parts of the if condition must be boolean conditions.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
In some languages TRUE is actually an int, mostly 1. But BASIC languages (VB for one), define TRUE as NOT FALSE (where not is the 2's complement), so TRUE is actually -1. If you are interacting with different languages, you have to take this into account -- for example, a Windows API return value in Visual Basic.
|
|
|
|
|
Not with C# compiler right? The compiler 'barks' and strongly recommends an explicit casting. Isn't it?
|
|
|
|
|
Yep, in C# you can't compare a bool to 1 or an int to true without explicit casting.
|
|
|
|
|
I dont think theres anything wrong with that, the compiler is going to optimize it anyways, the reason I like it is because is simpler to read than the alternative and simpler is always better in code. The only argument you could make that one shouldnt do that is if you mistype and put if(someBoolValue=true) and do an assignment instead of a comparision. The workaround to that is to either put constants on the left hand side if(true==someBoolValue), which you should already be doing so the compiler catches other similar mistakes (like null checks), or set the compiler warning level to 4, which I would also recommend you do to let the compiler detect other bugs.
|
|
|
|
|
Why, when you can write it like this:
column.DataType = System.Type.GetType("System.String");
(obviously, it's C#)
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Because it's faster... much faster.
I always hope the typeof() gets done at compile time, but I think that's not the case.
However, I just whipped up a little program to do one million of each, and the GetType took 00:00:17.1290068, whereas the typeof took a mere 00:00:00.0245439.
|
|
|
|
|
PIEBALDconsult wrote: ecause it's faster... much faster.
PIEBALDconsult wrote: GetType took 00:00:17.1290068
PIEBALDconsult wrote: typeof took a mere 00:00:00.0245439.
Either you missed my sarcasm, or I missed something else. So not only typeof shows intention much better (and is shorter), it's also faster. Excellent.
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
|
PIEBALDconsult wrote: It's a win/win/win
And those are very nice situations to be in
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
dnh wrote: typeof shows intention much better (and is shorter), it's also faster. Excellent.
Hey, what if I want potential runtime errors? You just need to write a unit test to make sure column.DataType == typeof(string).
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
I believe the typeof(T) operator is resolved at compile-time. Thus when you speak of how long "typeof took" you are really only measuring the time spent to loop, increment and check loop variable, and perform the assignment.
The MSDN documentation does make a distinction between types in general and the type objects that represent types (and which are, of course, themselves types). For example, the reference for the typeof() operator mentions:
Used to obtain the System.Type object for a type. A typeof expression takes the following form:
System.Type type = typeof(int);
It's clear that the System.Type is the "metatype", the type that is created for us but describes a type we created.
It also says to use GetType() to obtain the run-time type of an expression (although not the GetType overload discussed here, taking a string and finding a corresponding type in any loaded assembly in the appdomain!), and I take this as evidence in support of the "typeof() is resolved at compile-time" hypothesis.
|
|
|
|
|
I dont get the question, call me stupid if you will!
|
|
|
|
|
column.DataType = System.Type.GetType("System.String");
column.DataType = typeof(string);
Second line is more readable (IMO), less typing, and faster. There is absolutely no sane reason to write it the first way, hence my (not really a) question.
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
|
dnh wrote: less typing, and faster
And relatively less bytes to save on disk and less data to transfer across the wire during FTP and other copy operations.
|
|
|
|
|
It is not really a question, it is a programming horror the OP ran across.
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
System.String might not be defined in the current scope (reasons why, god knows)! Then typeof() wont work
|
|
|
|
|
GetType() takes more time because of its runtime evaluation right?
|
|
|
|
|
They are both in run time but there are JIT optimalizations... I've just found more on this topic in this[^] blog post.
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Hello guys.
I have links to asp pages on the HTML page and each time I clink on the link, it shows a save-open dialog box instead of opening the page.
Is there something I did wrong???
Please help!!
kagiso
|
|
|
|
|
I think you might just be in the wrong forum...
Try the web development forum instead.
|
|
|
|
|
|
Hi,
Have you started your web server? (For Example, IIS).
Try first to start your web server and then again check. It should work.
Because, for simple html pages you require only browser, but for asp pages you require a web server.
JKOZA
|
|
|
|
|
Please do not entertain people who feel lazy in getting to the correct forum and posting there. It is becoming an increasing menace elevating the debris all over.
Unless we have the whips in place and make the processes more stringent, this would become more and more a mess.
|
|
|
|