Do we actually need Methods in C# 3.0? We just need a powerful function type inference

DZone 's Guide to

Do we actually need Methods in C# 3.0? We just need a powerful function type inference

· Agile Zone ·
Free Resource

The big milestone of C# 3.0 got me thinking, what can we do more to improve the language. After a lot of interesting discussions and debates I got with people that care about the subject, I realized that it can be really interesting to remove some unimportant complexity of the language. This can be done by abstracting and generalizing some concepts. An example that I thought of for this blog post is Methods.

Do we need Methods?

Why do we need methods at all? For me, in C# 3.0, a method is just a special case of of a property that returns a function as value. What I mean is that this:

public string SayHello(){

return “Hello”;

is identical to

public Func<string> SayHello{
return ( ) => “Hello”;

In functional programming languages, functions are values exactly like int and string. They can be assigned to variables, passed as a parameter and return from other functions. This fits very well with the definition of a method I’ve just given. So what I am doing here is eliminating the need for the special case of method and using properties of function type instead. The syntax is acceptable (actually I can even argue that it is more readable). Moreover, adopting this approach buys us much more than removing one existing duplication in the language.

Benefits of generalizing Methods to Properties of function value type

The first thing I would argue about is readability, I guess it is more readable to describe methods as lambdas, but this can be considered being subjective, so I will rather focus on other practical benefits of the generalization:

Passing some methods implementations in the constructor:

Almost one year ago, I asked the C# team whether it can be possible to implement anonymous classes in C#. Anonymous classes ( quite different from Anonymous Types) is a neat feature that allows you to instantiate an instance of a Class that implements a desired contract. With anonymous classes you do not actually need to create the class, you just specify the contract you need to satisfy then you start implementing the logic.

Using our generalization we can do something similar (at least semantically). For Instance we can do:

new HelloWordContract( HelloWorldGreeter= (s=> Console.WriteLine(”Hello World “+ s)) );

Modifying an implementation at runtime only for the instance:

Manipulating functions exactly like values opens up for a very powerful mechanism of function passing. One very powerful functionality in Ruby is that you can change an instance’s method implementation at runtime. And that will only mutate the instance’s implementation and won’t touch to the class’ definitions. Using our generalization it becomes really easy to implement an extension method that changes the implementation (which is the property value) of a method for an instance.

public static ModifyImplementation<TObject,TFunc>(this TObject object, Func<TObject,TFunc> newFunctionValue){…} //here I pass the instance value in case needed by the function


Improving programming languages is not only about adding features, but it is sometimes about deleting some. I illustrated briefly in this post how we could, with the simple method generalization, benefit from a very powerful use of functions in C#. To support better this approach we still need a better type inference for functions types (delegates) and function types with more parameters ( something like Func<T1,T2,T3,…………………….Tn,R>.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}