0

In my wpf application, I used the ThreadTime as bleow.

TimerCallback tcb = new TimerCallback(TimerTask);
tmrdetectchanges = new Timer(tcb, "", 100, tmrinterval);  

private void TimerTask(object data)
{
    DetectChanges();
}

I just passed " " to TimerTask Parameter as I'm not using it. Is there any impact on my program? In my case , do i need to use this param? Please advise. Thanks.

2
  • I would use String.Empty instead of "". It's a tiny optimization. Commented May 4, 2011 at 2:19
  • @Red: Wrong; they compile to the same IL. Commented May 4, 2011 at 2:24

2 Answers 2

3

From MSDN:

public delegate void TimerCallback(Object state)

state

Type: System.Object

An object containing application-specific information relevant to the method invoked by this delegate, or null.

Just pass null instead.

Sign up to request clarification or add additional context in comments.

Comments

2

there is 'almost' no impact on your program. There's some but it's so minimal that you probably wouldn't even notice it. When you pass in "", it allocates a new string object on the heap so at some point garbage collector will have to find it and free it, which I'm sure it will relatively fast.

Since you are not using the user data param, just pass in null. Perfectly valid and saves those few CPU cycles (both for the call and for the garbage collection)

EDIT: As others have corrected me, there really almost pretty much no impact whatsoever passing a string instead of a null. However you will still save few CPU cycles if you pass in null. Passing in "" does generate one extra assembly instruction (at least on the calling side) that in your case would serve no purpose

Also note I only verified that in debug build and release could be optimized so there's no difference at all. I'm sure someone will correct me if that's the case :)

3 Comments

String literals are not allocated on the heap.
pretty sure they end up boxed if they are passed around as object type. So strings no, but by the time they end up in the callback, they are.
Reference type references are never boxed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.