|
|
A delegate is an objective way of representing a piece of executable code. In particular, delegates represent a pairing of a method and the instance that the method acts on, but can also represent static methods.
Going hand in hand with delegates are anonymous methods, which allow you to use the 'delegate' keyword to define the code that a delegate will execute, without having to actually define a method first (this is how I personally use delegates 99% of the time):
public delegate int SampleDelegate( int foo );
...
public void SomeMethod() {
SampleDelegate del = delegate( int foo ) {
return foo + 1;
}
MessageBox.Show( del.Invoke( 5 ).ToString() );
}
...will show "6" in a message box. That's basically all there is to delegates. They're not at all complicated when you have a proper understanding of what they do.
The above example is of a static delegate, which does not apply to an instance, but you can, for example, capture a delegate of an instance too:
MethodInvoker del2 = myObject.SomeVoidMethod;
At least I think that's how the syntax looks--like I said 99% of the time I just use anonymous methods. (MethodInvoker is a special delegate definition for void( void ) methods).
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
If it helps, its just the address of a function, but strongly typed.
Say you have a method in code such as: int Add(int x, int y) { some code }.
This just means "Push x and y on the stack, run "some code", and then pop the return value back off".
When you call the method directly the location of "some code" code is fixed by the compiler. Delegates are just a way of changing where "some code" points to at runtime. So a delegate is just a type that represents "Some method that has these parameters and returns this, and the code is here".
This allows things like a Button to have a click "event" (which is essentially a list of delegates). All the Button class has to know is that delegates in the list are to be called with (object Sender, ClickEventArgs args) and they don't return anything.
|
|
|
|
|
Delegates :
Deleagates is a referance type used to call same signature method.
signature means here return type and Parameters
1.single Cast Delegates
2.Multi Cast Delegates ( Can be used to call Same signature multiple methods)
Return type of Multicast delegates Mus be void bcoz it has invocaion list
+= oprator is used to define Multicast Delagates methods
To remove Methods from delegates -= is used
Thanks
Raja.S
|
|
|
|
|
When would I use a struct in C#?
Everything Makes Sense In Someones Mind
|
|
|
|
|
When you want to make a type with value type semantics.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.
when you instantiate a struct, it gets created on the stack. This will yield performance gains.
Vikas Amin
My First Article on CP" Virtual Serial Port "[^]
modified on Thursday, July 24, 2008 5:33 PM
|
|
|
|
|
vikas amin wrote: objects that behave like the built-in
Such as?
vikas amin wrote: enjoy their benefits
Which benefits?
vikas amin wrote: it gets created on the stack. This will yield performance gains
So, if I create a struct that has, say, 100 ints. That will be more performant than a class with the same?
What if I need to pass that object around? Say I need to pass it into a method or two?
|
|
|
|
|
Well of course, a nice neat stack is way better than a big untidy heap any day.
|
|
|
|
|
PIEBALDconsult wrote: Well of course, a nice neat stack is way better than a big untidy heap any day.
Nice reply!
|
|
|
|
|
Colin Angus Mackay wrote: So, if I create a struct that has, say, 100 ints. That will be more performant than a class with the same?
What if I need to pass that object around? Say I need to pass it into a method or two?
I believe that unless you use the 'ref' keyword, passing structs results in cloning, much like passing a string or int, so in this case it would probably be less performant if there is a lot of passing.
I'm not sure how the 'ref' keyword works on structs, but if it's like the built-in types this should avoid the above problem.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
vikas amin wrote:
objects that behave like the built-in
Such as?
--> By build-in I mean value type .
vikas amin wrote:
enjoy their benefits
Which benefits?
--> benefits of value type .
vikas amin wrote:
it gets created on the stack. This will yield performance gains
So, if I create a struct that has, say, 100 ints. That will be more performant than a class with the same?
What if I need to pass that object around? Say I need to pass it into a method or two?
--> Same solution don’t work in every scenario .
Its a fact that if you want to pass a large object to a method,
C# can use Ref keyword
--> What if you have 100 user defined variables to be created for a
short time say say for single calculation ,its a fact that Heap-
managed objects take more time to allocate, access, and update
than stack-managed ones
so you can use structure to speed up such process , i don't deny
that there are many more way to improve performance .
--> while using unmanaged code or say when use pinvoke to call native
code you set the structure attributes for performance
Thanks for the comments , please let me know if I am wrong some where
Vikas Amin
My First Article on CP" Virtual Serial Port "[^]
modified on Thursday, July 24, 2008 5:33 PM
|
|
|
|
|
vikas amin wrote: By using structs, you can create objects that behave like the built-in types
What he means is that you can create types with value type semantics.
vikas amin wrote: when you instantiate a struct, it gets created on the stack.
That is an over-simplification that can get confusing.
A struct is stored as inline data. If declared as a local varaible it's allocated on the stack, but if declared as a member of another type, it's allocated where that type is allocated. As a member of a class, it will be allocated on the heap along with the other members of that class instance.
vikas amin wrote: This will yield performance gains.
No, this may yield performance gains, if the struct is sufficiently small, and used in such a way that it takes advantage of how value types work.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
vikas amin wrote:
This will yield performance gains.
Guffa wrote:
No, this may yield performance gains
Yes you are right i should be "MAY" insted of "WILL" ,
Vikas Amin
My First Article on CP" Virtual Serial Port "[^]
modified on Thursday, July 24, 2008 5:33 PM
|
|
|
|
|
Consider this example class and struct
public class MyClass
{
public int Variable;
}
public struct MyStruct
{
public int Variable;
} They are identical apart from being a class/struct.
Now try this on these:
MyClass myClassA = new MyClass();
myClassA.Variable = 5;
MyClass myClassB = myClassA;
myClassB.Variable = 3;
Console.WriteLine(myClassA.Variable);
MyStruct myStructA = new MyStruct();
myStructA.Variable = 5;
MyStruct myStructB = myStructA;
myStructB.Variable = 3;
Console.WriteLine(myStructA.Variable); When the value of Variable is changed in myClassB it also changes the value of Variable in myClassA. This is because when we did MyClass myClassB = myClassA; it didn't copy the value in A to B - it referenced B to the same memory location that A was pointing to. That's passing by reference.
Structs are passed by value so a copy of myClassA is made and myClassB points to the copy.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Structure:
Lightweight object
Structure is allocated in Stack and passed as value type
Structure can implement interfaces
Instance can be created with new key word or not
Structure can contain Constructor
Class:
Simply it’s a abstract model used to define new data type
Fields is nothing but data in Class may be variable or other class object
Reference or object is created in heap
1. Referance Type:
Inherited from System. Object
Reference to memory location
Can be set to null
very much pointers in other languages
2. Value Type :
Inherited from System.Valuetype
Reference to value
Reference and value type both have advantage and disadvantage but it depends on
your requirement.
Thanks
Raja.S
|
|
|
|
|
I have used the following code in my media player for several years now and have incurred no problems. I recently updated to Mediaplayer 11 and this code is no longer recognized.
private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8 || e.newState == 1)
{
this.WMPlayer.Size = new System.Drawing.Size(325, 64);
}
if (e.newState == 8)
{
timer3.Interval = 5000;
timer3.Enabled = true;
}
}
Does anyone have an explanation and solution?
Complete source code that works under Mediaplayer 10 is available from my web site:
http://electriac.googlepages.com/wmplayer.exe[^]
modified on Tuesday, August 19, 2008 4:57 PM
|
|
|
|
|
I have uninstalled Mediaplayer 11 and reinstalled Mediaplayer 10 and all is well. So does this mean my program must use Mediaplayer 10!???
|
|
|
|
|
I have an application where I incremented the version on one of the projects now when I try and run it I get an Invalid Caste exception when I try to cast an object to a class that it is (specifically I have a class that implements an interface and I'm trying to cast it to that interface). I get the little message that pops up warning me that some assembly was compiled in release mode so I won't have debugging or some such. This pop up lists the assembly as 1.0.0.0 when I have set the assembly to 1.1.0.0. Why won't it use the new version? I have already removed all references and readded them in all projects that are dependent on this assembly.
|
|
|
|
|
Here is what they do in the Web.config to rebind the MS AJAX stuff, it should work the same for Castle if the assebmliesare compatible.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
xacc.ide - now with TabsToSpaces supportIronScheme - 1.0 alpha 4a out now (29 May 2008)
|
|
|
|
|
Thanks for the suggestion, I actually just figured it out. There is an XML file that lists all a bunch of types for this Query Builder tool we have and in this XML file it specifies the version, so all I had to do was update the version number in this file and it started working fine.
|
|
|
|
|
hi all.
i saw a application that show all of a .net application code from it exe file.
how can i lock my .net exe file?
sepel
|
|
|
|
|
sepel wrote: i saw a application that show all of a .net application code from it exe file.
These types of program are called disassemblers. Next, they dont show you all the code but the structure and not the logic.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Manas Bhardwaj wrote: Next, they dont show you all the code but the structure and not the logic.
Actually, they do show the logic. What they don't show is the exact original source code. There are no comments, some of the variable names may not be correct and so on. However, all the logic is still there.
|
|
|
|
|
Manas Bhardwaj wrote: they dont show you all the code but the structure and not the logic.
Not true. Reflector does show everything.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|