3

I have been working for some years in C# and one thing always bugs me. Delegates. They have no state, they are pointers to object methods, and the only way to go when dealing with threads or any other type of parallel programming technique that .net has to offer.

My main concern is that when programming in an object oriented style, the fact that delegates break the paradigm by breaking the notion that if you go Object Oriented then the design phase using UML and CRC should map to the language that you use.

So as the title asks, are delegates real objects or a programming anomaly, something that is required to be done outside the scope of Object Oriented Design.

Is it possible therefore for an object to be an object if it has no state and can a delegate be modelled in UML?

A well-known person once gave a public lecture on astronomy. He described how the earth orbits around the sun and how the sun, in turn, orbits around the center of a vast collection of stars called our galaxy. At the end of the lecture, a little old lady at the back of the room got up and said: "What you have told us is rubbish. The world is really a flat plate supported on the back of a giant tortoise." The scientist gave a superior smile before replying, "What is the tortoise standing on?" "You're very clever, young man, very clever", said the old lady. "But it's turtles all the way down! ---- Stephen Hawkins

I think this relates very well to objects and the fundamental types they sit upon. Can you really say its objects all the way down?

17
  • 2
    Delegates certainly have state, at least so much as strings and integers have state.
    – Gabe
    Oct 19, 2010 at 1:30
  • 9
    You lost me at "they have no state". You keep repeating this religiously, but it's a pretty poor religion to put your faith in, since it's demonstrably false.
    – Ben Voigt
    Oct 19, 2010 at 2:45
  • 1
    I don't use "UML" or "CRC". Am I a bad OO programmer? Oct 23, 2010 at 11:07
  • 1
    @WeNeedAnswers: Similarly you cannot say that delegates are not OO because they do not support inheritance. Inheritance is not a principle of OOP, it is one way of implementing OOP principles of polymorphism and reuse. Delegates may not have inheritance, but they do support subtype polymorphism and also reuse via composition. Just because other forms of polymorphism and reuse do not fit into your nice UML world does not mean that they are not OO, it means that UML is not as "universal" as advertised.
    – Ben Voigt
    Oct 23, 2010 at 15:39
  • 2
    But OCP (which I called "reuse" in my previous comment) can also be achieved by composition, in fact many computer scientists believe that OCP through composition is superior to OCP through inheritance. So "delegates don't have inheritance" does not prove that they aren't objects.
    – Ben Voigt
    Oct 23, 2010 at 23:29

5 Answers 5

16

They're real objects.

When you're modeling with UML it's common to use interfaces. Do they have state? No. Are they any less object-oriented? Of course not.

Of course a delegate can be modeled in UML.

I think they represent a more functional approach, because they are functions that are first class objects. That might concern you, but it doesn't detract from the fact that they are indeed objects.

27
  • Interfaces are not objects though, they are design patterns. You implement an interface, making sure that your object adheres to the pattern. How would one model a delegate in UML, could you point me to an example. thanks in advanced. Oct 19, 2010 at 1:26
  • I said it was common to use interfaces in UML. And no, they aren't design patterns. They are objects with all pure virtual functions. You'd model a delegate as an object with all the methods in the base class. You sound like you're really hung up on UML. It's not a useful obsession.
    – duffymo
    Oct 19, 2010 at 1:37
  • Sorry, when I say design pattern I should have made it clear, they are more like patterns of usage not "Design Patterns" in the common sense. You don't implement an interface as an object. What base class, you thinking of Java there, delegates are implemented differently in C#. Oct 19, 2010 at 1:46
  • As for my obsession in UML. UML is a programming language, just not been hooked into a code generator as of yet. It will come. When a system fades away into obscurity (Cobol for example), hopefully the schematic diagrams I draw up in UML will still be around to describe in a metaphysical stance of what was analysed and what we tried to achieve. If I modelled a delegate as you stated, it would not be an accurate representation in c# code. Oct 19, 2010 at 1:48
  • 1
    I think you need to learn a lot more about objects. Your view of what is and is not an object is very narrow, indeed.
    – duffymo
    Oct 19, 2010 at 2:43
7

It's become clear that this point isn't going to get across without somebody wielding the closure hammer. No, not the "close this question" hammer.

Action @ICanHazState()
{
    int callcount = 0;
    Action retval = delegate {
       System.MessageBox("I haz bin calld " + callcount.ToString() + " tims b4");
       if (++callcount > 5) throw new InvalidOperationException("Call limit exceeded.  Kaboom!");
    };
    return retval;
}

Complete compilable example here

More complex example (requires C# 4 e.g. Visual Studio 2010)

26
  • 1
    I think your hammer would be more effective if you show the output of a small program using the closure.
    – Gabe
    Oct 19, 2010 at 3:03
  • 1
    @WeNeedAnswers: It's just a little delegate wanting to show that it has state.
    – Ben Voigt
    Oct 19, 2010 at 3:20
  • 1
    I love that this answer destroys the obvious pro dynamic languages flame bait the OP tried.
    – eglasius
    Oct 19, 2010 at 4:39
  • 1
    The complex example needs C# 4, it will not compile in the version of mono used on ideone. Was that not clear from my post? Also, in case you didn't notice, those out parameters, which you called objects, are also delegates. I think that means I win.
    – Ben Voigt
    Oct 23, 2010 at 15:42
  • 1
    Well, delegates should be very effective in continuation-passing style, which requires tail calls to avoid exhausting the stack. I don't know if you can convince the C# compiler to generate tail calls to delegates. Eric Lippert, one of the C# designers, wrote about CPS recently part 1, part 2.
    – Ben Voigt
    Oct 24, 2010 at 3:12
3

From wikipedia (Object (computer science): In the domain of object-oriented programming an object is usually taken to mean a compilation of attributes (object elements) and behaviors (methods or subroutines) encapsulating an entity. Delegates all are subclassed from the Delegate class in .Net. The Delegate class certainly has attributes (i.e. properties): Method & Target. The class also has behaviors (methods): Combine, CreateDelegate among others.

Delegates are not pointers, as a simple pointer to a method could not encapsulate the 'this' pointer, which delegates do. As Gabe pointed out, delegates also have state, they could be empty (in which case invoking them does nothing) or they could have one or more methods in their list.

Having satisfied the criteria for an object, they are objects.

Q.E.D.

7
  • Who's Gabe? None of the folks responding here go by that tag. Wait, now I see - first comment.
    – duffymo
    Oct 19, 2010 at 2:16
  • could you explain that bit about 'this'? Oct 19, 2010 at 2:35
  • WeNeedAnswers: When you call a method like employee.GetSalary(), the GetSalary method gets called with the value of employee as an invisible first parameter. Inside of the GetSalary method, you access that parameter with the keyword this. When you create a delegate for employee.GetSalary, it creates an object with both a pointer to the GetSalary method and the value for employee.
    – Gabe
    Oct 19, 2010 at 2:44
  • Creates or Calls, I thought that it just calls the method on the already created object on the current thread that its operating on. Where does the 'this' come into it, I thought that would be a given, given that the object being called knows exactly the context to which it is being called in and in which scope. Oct 19, 2010 at 2:50
  • WeNeedAnswers: Creating a delegate creates an object holding a function pointer and a parameter. Calling that delegate calls that function pointer with the included parameter.
    – Gabe
    Oct 19, 2010 at 3:04
2

Yes. They don't break the object oriented paradigm. They just embrace and extend it ;)

2

Define "object."

Your definition seems to be that to be an object, 'thing' must have mutable state and be inheritable.

By that definition, delegates are not objects. However, I consider that definition to be extremely, extremely limited. Entire categories of 'objects' in real-world applications fail by that definition. Functors, to start with. Many decorator types would also fail by that definition, as well as things like filter objects.

If you go back to Smalltalk, for instance, both true and false are objects. By your definition, they would not be.

5
  • Exactly right. Java enums wouldn't qualify as objects either, yet they inherit from Enum. The JVM certainly sees them as objects.
    – duffymo
    Oct 23, 2010 at 2:58
  • Same with C#; no different. csharp-station.com/Tutorials/lesson17.aspx
    – duffymo
    Oct 23, 2010 at 10:40
  • Is a string an Object, now come on, be honest, you think that a string is really not an array of chars in disguise. Enums, sugar syntax for a read only associative array? Oct 23, 2010 at 12:25
  • @WeNeedAnswers: Fine, then, as I said in my answer - define "object."
    – kyoryu
    Oct 23, 2010 at 20:46
  • @kyoryu, I think if you can duplicate something in a language using the constructs of the language, including the syntax sugar that the syntax of the language uses, then I would say that this is an object. From that definition, if you can not duplicate the functionality, the syntax sugar or replace of such article in code then this is not an object. Under this definition, a String, an Enum and a delegate are not objects. As for objects outside of the language specifics, take a look at the OMG's definition, or take a look at the marvellous David Wests book on Object Thinking. Oct 23, 2010 at 21:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.