Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not possible to declare a function with VAR return type?

In C#, we have var data type but we can't use it as functions return type.
Why this is not possible?

public var myFunction()
{
    var = some operations
}
like image 599
Dr TJ Avatar asked Nov 09 '10 09:11

Dr TJ


People also ask

Can var be used as return type?

You simply can not use var as a return type.

Can var be used as return type in C#?

you can't return var.

Can we declare method with return type without?

You need to add the void modifier to your method if it does not return a value. The method without the modifier is the constructor and is correct.

Can you declare var keyword as a parameter type in the function?

var is a generic variable type but you cannot use it in as a parameter nor a return type.

Can you use VAR as a return type in C++?

Yes, it's boilerplate, but it's simple. It will make your solution infinitely more maintainable than passing around sequences of anonymous types (yes, that's an option; do not do it ). You simply can not use var as a return type. var says to the compiler, look, you do your thing and you figure out what the type is.

How to declare a function with an array return type?

To declare a function with an array return type, set the return type of the function to an array right after the function's parameter list, e.g. function getArr (): string [] {}. If the return type of the function is not set, TypeScript will infer it.

Should you stop using VAR for declaring variables?

Stop using var for declaring variables !!! If you are new to JS or been with it for quite some time then for the most part you probably be using var to declare your variables which is fine but it's not the most efficient & preferred way of declaring variables. But why? And what should I use instead of var? Let's find out!

How many variables can be declared inside a function in C?

You can declare any number of variables inside the function, but function can return only one value. If you want function to return more than one value, there’s a concept called pointers in c. Though it is pretty difficult to learn. it is helpful for your future programming.


2 Answers

I believe it's partly due to the design of the compiler. Eric Lippert blogged about why fields can't use implicit typing, and I suspect some of the same arguments hold for methods.

But you could easily end up with ambiguity anyway. For example:

var Method1(bool callMethod2)
{
    return callMethod2 ? Method2() : null;
}

var Method2()
{
    return Method1(false);
}

What should the type be here?

A simpler example:

var Method1(bool throwException)
{
    if (!throwException)
    {
        return Method1(true);
    }
    throw new Exception("Bang!");
}

Admittedly this sort of ambiguity could simply be disallowed, but I suspect that the design team felt that the added complexity of both design and implementation wasn't worth the benefit. Don't forget that they're running with limited resources - given a choice between var for methods and async/await, I'd pick the latter in a heartbeat. (Admittedly there are other features I'd have picked instead of dynamic, but that's a different matter...)

Note that return type inference is performed for lambda expressions, so the very idea of it isn't crazy. For example:

IEnumerable<string> x = new[] { "x", "y", "z" };

var result = x.Select(s => { return s.Length; }); // Long form

There the compiler infers the complete type of the lambda expression when it performs overload resolution on Select, converting it to a Func<string, int>. It's not inconceivable to apply the same ideas to methods - just complicated.

like image 66
Jon Skeet Avatar answered Sep 20 '22 17:09

Jon Skeet


var is NOT a datatype in C#. That's why you cannot use it as a return parameter. The compiler infers the type at compile time from the right handside of the assignment and bearing in mind that it is known at compile time you need to use the real type as return value. In C# 4.0 you could use the dynamic type:

public dynamic myFunction()
{
    var = some operations
}
like image 45
Darin Dimitrov Avatar answered Sep 22 '22 17:09

Darin Dimitrov