Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP - Message Passing in C#

What is the example of the OOP concept 'Message Passing' in C# (calling methods/Passing parameters/Firing Events/Handling Events/???) and why is it called message passing?

like image 381
user366312 Avatar asked Jul 27 '09 16:07

user366312


People also ask

What is the message passing in OOP?

What is message passing and why it is used? Message Passing in terms of computers is communication between processes. It is a form of communication used in object-oriented programming as well as parallel programming. Message passing in Java is like sending an object i.e. message from one thread to another thread.

What is message passing example?

Web browsers and web servers are examples of processes that communicate by message-passing. A URL is an example of referencing a resource without exposing process internals. A subroutine call or method invocation will not exit until the invoked computation has terminated.

Is C good for OOP?

Yes, you can. People were writing object-oriented C before C++ or Objective-C came on the scene. Both C++ and Objective-C were, in parts, attempts to take some of the OO concepts used in C and formalize them as part of the language.


3 Answers

Method calls.

It's called message passing to distinguish it from the imperative notion of "calling a function", and to reinforce the idea that the receiving object decides what to do. On the call site, you just tell the receiver the "message".

An example would be methods that are abstract in a class, and implemented in subclasses; or implementations of an interface method. When you call e.g. GetEnumerator() through a variable of type IEnumerable, you don't know at the call site what code should actually be executed.

like image 69
millimoose Avatar answered Sep 18 '22 23:09

millimoose


It is called message passing to distinguish it from passing parameters.

A major benefit of passing a message is that you can change the contents of the message without changing the signature of the method recieving the message.

Another is that several methods may need the same information, it can therefore be defined and changed in the same place.

like image 39
Shiraz Bhaiji Avatar answered Sep 17 '22 23:09

Shiraz Bhaiji


There are some who feel that message passing and method calls are different. We use the term interchangeably, but the meaning is subtle.

In smalltalk, message passing was run time bound, and the object had a way to determine if it could handle a message that wasn't explicitly defined as a method. Ruby calls this method_missing. Methods in C++ in particular are bound at compile time, with no way to dynamically add ways to handle more messages. C# 4.0 has a mix, once you start throwing dynamics around.

There's another school of message passing, Erlang believes all message arguments need to be decoupled in state. That is, they are either immutable or copies.

like image 28
Ball Avatar answered Sep 21 '22 23:09

Ball