Click here to Skip to main content
15,915,755 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I am just confused around strange issue when subscribing my method to delegate:

Work ok on below scenarion:

Public void Test()
{

//Func<int,> objFunc = null;  ---------Line 1
//objFunc += (x) => { return x == 5; };--------Line 2

//Func<int,bool> objFunc += (x) =>{ return x == 5; };---------Line 3

}
In above code snippet, If I comment Line 3, then it compiles will. But if i comment Line 1 , Line 2 and Uncomment Line 3, it does not compiles.

What is the issue? arent both the things same ?

please help me on this regard.

Thanks!!!

[added code block for better readability]
Posted
Updated 4-Apr-11 19:46pm
v2

There is no such thing "subscribing my method to delegate".

You create a delegate instance and add an anonymous method to its invocation list.

System.Action<int, string> myAction = null;
myAction += (number, format) => {
    System.Console.WriteLine(format, number); //for example
};


Here System.Action<int, string> is a delegate type, myAction is a delegate instance.
You should understand, that adding one more method to the invocation list change referential identity of the variable.

System.Action<int, string> myAction = null;
myAction += (number, format) => {
    System.Console.WriteLine(format, number); //for example
};
System.Action<int, string> oldAction == myAction;
myAction += (number, format) => { /*...*/ };

//at this moment, oldAction != myAction; object.ReferenceEquals(oldAction, myAction) == false;


So, when you do this operation (+=), a brand new delegate instance is created and assigned to a variable. In other words: delegate instances are not mutable. Why? For the same reason as strings: thread safety.

Any questions?

—SA
 
Share this answer
 
v2
Comments
Rahul83 5-Apr-11 3:57am    
Thanks!!

But when i just copiend below sample code in VS inside some test() method:
System.Action<int, string=""> myAction += (number, format) => { System.Console.WriteLine(format, number);

I was showing error, "number" and "format" does not exist in current context.
But when i will do as below:
System.Action<int, string=""> myAction =null;
myAction += (number, format) => { System.Console.WriteLine(format, number);
It works (same as initial problem i posted)

Where m i doing worng?
Thanks!!
Sergey Alexandrovich Kryukov 5-Apr-11 13:26pm    
Please see my update.
Sorry, this is my fault: I forgot it's a stack variable which needs initialization, unlike a member; and I did not check it up on a compiler, sorry about that.
Now it works.

(Your code above still has the same misprint ",>" should not compile.)

Thank you for pointing it out.

--SA
Albin Abel 6-Apr-11 16:16pm    
So that is called thread safety. Good explanation. My 5
Sergey Alexandrovich Kryukov 6-Apr-11 18:29pm    
Yes, this is some part of thread safety taken with performance considerations.
Thank you, Albin.
--SA
Line 1 has the wrong syntax. That is why Lines 1 and 2 are erroring out. It should be

Func<int,bool> objFunc = null;
 
Share this answer
 
v2
I think the problem here us that before telling the comiler what objFUnc is , u r trying to assign functinality to it..

line1,lin2 makes sure that once delegate is declared and then functionality is assigned to it , many functionalitites similar to line2 can be added...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900