FUNCTIONS AND METHODS

C# is an object oriented language. Not just that, it’s a comparatively pure object oriented language. The degree of object orientation can’t really be measured, but different programming languages adhere to the ideas of certain programming philosophies to a certain extent, and rarely do they go all the way. In this case the interesting point is that C# doesn’t allow any functions outside of classes. This is a big difference compared to C++ — the first object oriented programming language for many “mainstream” programmers — which allows functions to live outside of classes, mainly for backward compatibility with C code. It can be argued that since C# supports classes with static members, and even fully static classes are supported since version 3.0, imperative code is simply hidden behind object oriented terminology as needed. However, looking at the understanding of C# evident with the majority of programmers, the C# design team succeeded in creating a perception of an almost purely object oriented language.

Because functions in C# can only exist within classes, they are typically called methods. Methods can accept a number of parameters, and they can have a return value. Some people might argue that they always have a return value, but the return value can be of type void, which basically means that the method doesn’t return anything. Just in case you haven’t ever seen a C# method before, here’s one:

class MyClass {

  int Square(int x) {

    return x * x; ...

Get Functional Programming in C#: Classic Programming Techniques for Modern Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.