|
Looking for some answers on MSDN I ran across the following code.
If (null != email)
Is that legal in C#, and even if it does compile, is it logical?
Shouldn't it more likely read:
If (email != null)
Best,
Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes Toasty0.com
|
|
|
|
|
This is a wierd convention that older C programmers follow. It is perfectly legal.
The reason that it is done, is that in C, it is legal to say if (email = null) , (note the single equal sign) and it will always evaluate to true. So, to prevent possible errors caused by mistypes, C programmers switch the arguments, i.e. if (null = email) . This second version is not legal syntax, and will give a compiler error. The programmer can then fix it by changing it to if (null == email) .
This is not necessary in C# (but neither is it illegal), because the C# compiler will complain at if (email = null) .
my blog
|
|
|
|
|
|
I'd expect it to be true, because the assignment would not fail.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
The condition isn't checking the assignment, it would check the resultant expression. In ths case, that'd be email . The code breaks apart like so:
email = null;
if (email)
{
} In C#, however, the above expression isn't valid like it would be in C/C++ (where NULL is defined as 0). You would get an error in this case, but not because you'd be using an assignment operator instead of an equivalence operator.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Oh, OK. Obviously I had not tried it.
Heath Stewart wrote:
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
Cool. Do you know anything about DX9, specifically the AudioVideoPlayback namespace ? Was it released by mistake ? I've had a lot of fun figuring out it's many flaws.....
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
I've worked with DX9 a little, but I admit I don't know a whole lot about it (mostly from a graphics manipulation regard, not API use).
I recommend reading Managed DirectX 9 Kick Start : Graphics and Game Programming[^]. It's a very good book and you'd probably have a better time with it. Like I said, I understand all the API stuff, but matrices - when it comes to graphics - always seem to elude me. I will say, however, that you're articles have helped clear a few things up.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Heath Stewart wrote:
I recommend reading Managed DirectX 9 Kick Start : Graphics and Game Programming[^].
I have it. I assume the rest of the book is better, the bit on audio video playback is 3 pages which glosses over the API, tells me that Microsoft does not regard it as a full implimentation, and it's not going to be updated. It doesn't cover any of the bugs in the API, I presume the author has never actually used this part of DirectX9, and felt they needed to cover it, so they skimmed the docs.
I'm writing an article on all the ways that the AudioVideoPlayback namespace fails to perform as expected, and a wrapper class that will make it a little bit close to usable. I've built an entire project around DX9, and I'm kind of wishing I hadn't.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Steven Campbell wrote:
The programmer can then fix it by changing it to if (null == email).
Ok, but how can null be "equal to" anything? This has got to be a concept I'm not able to fathom or I am just unfamiliar with it. And, for now, it just seem logical.
Best,
Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes Toasty0.com
|
|
|
|
|
Any reference object - like email (presumably a string, which is a reference type) - is null if it doesn't reference an object. So, it's "value" is null.
string email = null;
null == email;
email = "you@codeproject.com";
null == email;
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
if 2+2 = 4, then 4 = 2+2. The concept is the same. The two values are checked for equality, that the constant value is on the left makes no difference to the compiler, and in C++ was a good was to avoid a bug that can be a nightmare to find. I know, I still sometimes write code like this, a habit I formed in my C++ days.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Ok, thanks Heath and Christian. I think I understand what's going on with that now.
Best,
while(null==jerry)
{
null++;
return;
}
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes Toasty0.com
|
|
|
|
|
To expand on what Steven said, the two are the same because it's a simple comparison. The IL generated from about would be:
ldnull
ldfld email
beq Equal
Equal:
It doesn't matter if you load null or the field (or variable, whichever) first, you're still comparing the value of two objects.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Thanks for the answers all. Would this also hold true for the is-equal-to '==' also?
Best,
Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes Toasty0.com
|
|
|
|
|
OMG! I just woke up and realized what a stupid question that was.
That's what I get for watching the grandchildren and coding at the same time...
Best,
Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes Toasty0.com
|
|
|
|
|
In C++, it's possible to write:
if (x = 0)
when you mean to write
if (x == 0)
Because of that, some developers invert the order of what they write, as:
if (0 = x)
is illegal, while
if (0 == x)
is legal.
This is not a problem in C#, where if (x = 0) is prohibited.
|
|
|
|
|
Hi
I need a logging framework that let me log asynchronously.
Do you have any suggestion ?
I tried to figure out if log4net does all the logging asynchronously. Does it depend on a specific Appender or all logging is done asynchronously ?
I did a test in which I log 1500 time the same string.
- Log4net with FileAppender is doing it in approx. between 100 and 200 miliseconds;
- Log4net with BufferingForwardingAppender combine with FileAppender is doing it in approx. 2000 miliseconds;
- System.IO does it in approx. 10 miliseconds.
I also tried with EIF using my own EventSink sending all request in a MSMQ queue but using EIF is not as easy to deploy as log4net so...
Any suggestion would be appreciate
|
|
|
|
|
The EIF offers more than just logging using the EIF.
What you're logging to, however, greatly matters unless you're queuing requests in order and flushing them to disk or some other sequential data store.
Note that a simple solution is to define a delgate on the logging method and execute that asynchronously. The backing store matters, however, because doing things this way with a non-thread-safe backing store could cause major problems.
For example:
internal delegate void Log(string message, string category);
Log log = new Log(Trace.WriteLine);
log.BeginInvoke(message, category, null, null); It's an overly simplified example, but hopefully gives you the idea.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Log4Net looks suspiciously like Log4J. Easy to use; but a hog and will slow your program down. In Log4J there are asynchronous appenders, look for the same in Log4Net. You should see some increase in speed; but I wouldn't expect any logging framework to be faster or even as fast as System.IO.
Daniel
|
|
|
|
|
Thanks for your reply Daniel
I have a couple of question regarding what you just said:
1- Can you tell what are the appender that you know is asynchronous (in Log4J at least)
2- Why would you expect System.IO to be faster ? I understand that there is a cost (overhead) to use this kind of framework but if the appender is sending message (logging event) to a structure that it is store in memory, I expect it to be faster than an IO on disk. Am I wrong Daniel.
If I recall my first post, this is exactly what I did using EIF and a specific EventSink (appender). My EventSynk only take the logging event and put it on an MSMQ and return very fast (but telling that remind me that I did not save the metrics for that one, so how fast it realy is... I'll tell you)
Anyway, Thank you to both of you who reply to this post.
I can't believe that in 2004 I'm still worry about logging
Thanks again
Michel
|
|
|
|
|
1. In Log4J you can use something called AsyncAppender. It takes references to already instantiated Appenders and uses them to write asynchronously.
2. Your are not wrong about writing to memory being faster than writing to disk.
But, if your ultimate goal is to have another layer of abstraction manage logging to disk for you faster than using System.IO you probably won't find anything that can. It won't be faster because that extra layer of abstraction will necessarily add some time to processing your logging plus writing the information to disk later.
Daniel
|
|
|
|
|
Hi there,
I'm facing the following problem: I'm writing test cases (using NUnit) for my classes and I have to test for example if properties will throw an Exception when there is an attempt to assign invalid values.
Say I have a Car class with a Speed Property I want it to throw an Exception if the user attempts to assign a value greater then 90.
My test code looks somewhat like this:
Car c = new Car();
bool bFailed = false;
try { c.Speed = 200; }
catch { bFailed = true; }
Assert.IsTrue(bFailed, "Property Speed accepts incorrect values");
When you have a ton of TestCases to write and a lot of Properties to test, this becomes quite cumbersome.
My question is, can this code be somewhat improved? Is there a more generic approach?
Thanks in advance.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
try it like this...
Car c = new Car();
try {
c.Speed = 200;
Assert.Fail("Property Speed accepts incorrect values");
}
catch ArgumentException
{ }
Be sure to specify the type of the exception (not just Exception ).
my blog
|
|
|
|
|
Steven's answer is good, but this should be better:
[ExpectedException(typeof(IncorrectSpeed))]
public void TestSpeed()
{
Car c = new Car();
c.Speed = 200;
}
NUnit will allow you to tell the test exactly what exception should be thrown by the individual test as an attribute to the test method. The framework will pick up if the exception wasn't thrown and fail the test.
Of course if you want to make it more fine grained than that, say if you are expecting a SqlException with a specific error Number then Steven's code will allow you to pick up on that.*
* However, there is a newer version of NUnit that I've not used yet and it may have that feature already.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!
|
|
|
|
|