Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are c# anonymous methods object oriented?

I'm just checking out anonymous methods (in c#)--part of me likes the flexibility and short-hand, but I'm also concerned that it may make the code harder to read.

It also occurred to me that this construct seems to go against some of the o/o paradigm. Do you consider anonymous methods to be in-line with object oriented principles?

like image 281
alchemical Avatar asked Aug 17 '09 17:08

alchemical


People also ask

Is there a C+?

C+ (grade), an academic grade. C++, a programming language. C with Classes, predecessor to the C++ programming language. ANSI C, a programming language (as opposed to K&R C)

Are C and C++ the same?

While both C and C++ may sound similar, their features and usage are different. C is a procedural programming language and does not support objects and classes. C++ is an enhanced version of C programming with object-oriented programming support.

What is C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

lambda (anonymous methods) is from the functional paradigm. That doesn't mean it is good or bad! If it fits the problem then use it, if it doesn't don't. OOP is not a goal, good code is the goal. I hate when people try to force a single paradigm down the throat, like in Java for example. C# is going in the right direction (IMHO), so it is becoming a multiparadigm language.

like image 149
Khaled Alshaya Avatar answered Oct 21 '22 00:10

Khaled Alshaya


If you'd like to think of them with respect to Object Oriented design, they're merely syntactic sugar for some anonymous class which contains a method which gets invoked. In fact, Java does it with the longer winded final class. C# chose the shorter method. Both are valid and well within the bounds of Object Oriented design.

Lambda expressions are also no less Object Oriented than delegates. IMHO, lambda expressions fall into an almost entirely orthogonal study of programming from OOP: functional versus procedural.

So, use the right tool for the job be it lambdas, delegates, anonymous classes, objects, monads, etc ad nauseam. Your goal should be to have the right code to solve the right problem.

like image 40
user7116 Avatar answered Oct 21 '22 01:10

user7116