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

后端 未结 2 1918
予麋鹿
予麋鹿 2020-12-19 12:24

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 = s         


        
2条回答
  •  执念已碎
    2020-12-19 12:36

    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
    }
    

提交回复
热议问题