Click here to Skip to main content
15,913,089 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Help Urgent - OH NO ITS NOT!!!!!!!!!!!!!!! Pin
Ashfield15-Oct-08 8:38
Ashfield15-Oct-08 8:38 
JokeRe: C# Help Urgent - OH NO ITS NOT!!!!!!!!!!!!!!! Pin
Giorgi Dalakishvili15-Oct-08 7:01
mentorGiorgi Dalakishvili15-Oct-08 7:01 
JokeRe: C# Help Urgent Pin
nelsonpaixao15-Oct-08 14:27
nelsonpaixao15-Oct-08 14:27 
QuestionPlease Help Me C# Pin
boiDev15-Oct-08 4:57
boiDev15-Oct-08 4:57 
AnswerRe: Please Help Me C# Pin
Paul Conrad15-Oct-08 6:47
professionalPaul Conrad15-Oct-08 6:47 
QuestionC# Help Pin
boiDev15-Oct-08 4:46
boiDev15-Oct-08 4:46 
AnswerRe: C# Help Pin
Lea Hayes15-Oct-08 11:31
Lea Hayes15-Oct-08 11:31 
QuestionDelegates are trying to make me look stupid Pin
Rob Philpott15-Oct-08 4:17
Rob Philpott15-Oct-08 4:17 
Afternoon everyone. Crap weather we're having today...

Now then, what's going on here?

using System;

namespace Confusion
{
	public delegate void ThingDelegate();

	public abstract class A
	{
		public virtual void Thing()
		{
			Console.WriteLine("A::Thing");
		}
	}

	public class B : A
	{
		public override void Thing()
		{
			Console.WriteLine("B::Thing");
			ThingDelegate del = base.Thing;
			del.Invoke();
		}
	}

	public class C : A
	{
		public override void Thing()
		{
			Console.WriteLine("C::Thing");
			ThingDelegate del = base.Thing;
			del.BeginInvoke(null, null);
		}
	}

	class Program
	{
		static void Main(string[] args)
		{
			B b = new B();
			b.Thing();

			Console.ReadLine();

			C c = new C();
			c.Thing();
			
			Console.ReadLine();
		}
	}
}


If you run this simple console application what's the output? If you're as dim as me, you'd expect:

B::Thing
A::Thing
C::Thing
A::Thing

What you actually get is:

B::Thing
A::Thing
C::Thing
C::Thing
C::Thing
...
... to infinity and beyond.

eg. Invoke and BeginInvoke resolve to different methods. Can anyone explain why?
Confused | :confused:

Regards,
Rob Philpott.

AnswerRe: Delegates are trying to make me look stupid Pin
Ennis Ray Lynch, Jr.15-Oct-08 5:35
Ennis Ray Lynch, Jr.15-Oct-08 5:35 
GeneralRe: Delegates are trying to make me look stupid Pin
Rob Philpott15-Oct-08 5:37
Rob Philpott15-Oct-08 5:37 
GeneralRe: Delegates are trying to make me look stupid Pin
Ennis Ray Lynch, Jr.15-Oct-08 6:14
Ennis Ray Lynch, Jr.15-Oct-08 6:14 
AnswerRe: Delegates are trying to make me look stupid Pin
Le centriste15-Oct-08 6:25
Le centriste15-Oct-08 6:25 
GeneralRe: Delegates are trying to make me look stupid Pin
Rob Philpott16-Oct-08 1:37
Rob Philpott16-Oct-08 1:37 
AnswerRe: Delegates are trying to make me look stupid Pin
Simon P Stevens15-Oct-08 23:32
Simon P Stevens15-Oct-08 23:32 
GeneralRe: Delegates are trying to make me look stupid Pin
Rob Philpott16-Oct-08 1:33
Rob Philpott16-Oct-08 1:33 
Questionis it such a thing LinkedLists in C# ? Pin
nesfrank15-Oct-08 3:38
nesfrank15-Oct-08 3:38 
AnswerRe: is it such a thing LinkedLists in C# ? Pin
Simon P Stevens15-Oct-08 3:45
Simon P Stevens15-Oct-08 3:45 
AnswerRe: is it such a thing LinkedLists in C# ? Pin
Kevin McFarlane15-Oct-08 3:46
Kevin McFarlane15-Oct-08 3:46 
AnswerRe: is it such a thing LinkedLists in C# ? Pin
Giorgi Dalakishvili15-Oct-08 3:46
mentorGiorgi Dalakishvili15-Oct-08 3:46 
GeneralRe: is it such a thing LinkedLists in C# ? Pin
nesfrank15-Oct-08 6:29
nesfrank15-Oct-08 6:29 
GeneralRe: is it such a thing LinkedLists in C# ? Pin
Giorgi Dalakishvili15-Oct-08 6:40
mentorGiorgi Dalakishvili15-Oct-08 6:40 
GeneralRe: is it such a thing LinkedLists in C# ? Pin
nesfrank15-Oct-08 7:51
nesfrank15-Oct-08 7:51 
GeneralRe: is it such a thing LinkedLists in C# ? Pin
Dan Neely15-Oct-08 7:09
Dan Neely15-Oct-08 7:09 
GeneralRe: is it such a thing LinkedLists in C# ? Pin
nesfrank15-Oct-08 7:50
nesfrank15-Oct-08 7:50 
QuestionProblems of losing handle to stdcall-api from C#-DLL Pin
J. Holzer15-Oct-08 3:29
J. Holzer15-Oct-08 3:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.