0

I have the following code as part of my project. There is other code in there that can be commented out for the purposes of this exchange.

namespace Project.HttpHandlers
{
    public class Web : IHttpHandler
    {
        /// <summary>
        /// Gets a value indicating whether another request can use the 
        /// <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <returns>
        /// true if the <see cref="T:System.Web.IHttpHandler"/> instance 
        /// is reusable; otherwise, false.
        /// </returns>
        public bool IsReusable => false;
    }
}

Visual Studio throws an error on the "public bool IsReusable => false;" line stating that "; expected".

When highlighting the intellisense error over the => operator I get "unexpected end of declaration".

If I change it to "public bool IsReusable = false;" the error goes away. I don't entirely know the function of this line and why there is a lambda operator there so I do not want to do that. I do know it is compilable on a coworkers machine and I see it referenced elsewhere on the web.

I seem to be missing a reference or something else in Visual Studio but I cannot find it.

Keith
  • 68
  • 6
  • 3
    Are you sure your iis instance supports C#6? I have never used it myself but I think you need to use [asp.net core](https://docs.asp.net/en/latest/intro.html) to be able to get those features. – Scott Chamberlain Oct 05 '16 at 13:21
  • @ScottChamberlain Wouldn't it be the version of VS since C#6 is the compiler. – juharr Oct 05 '16 at 13:23
  • The same error appears when trying to use the new syntax with VS 2013, so I guess your compiler or IDE doesn't support C# 6. – Dirk Oct 05 '16 at 13:23
  • @juharr it depends on if the OP is deploying a dll to IIS or is deploying a .cs file and letting the server compile it. If the server is compileing it the version of VS you used to write it does not matter. – Scott Chamberlain Oct 05 '16 at 13:24
  • @juharr - depending on which "model" of ASP.Net one is using, some or all of the code isn't compiled until the website starts up or a particular page is requested. – Damien_The_Unbeliever Oct 05 '16 at 13:24

3 Answers3

1

This is c# 6 syntax for expression bodied members.

Syntax for < C# 6:

 private readonly bool _isReusable = false; 
 public bool IsReusable 
 {
     get {return _isReusable; } 
 }

EDIT: corrected as commented by Scott Chamberlain.

L-Four
  • 13,345
  • 9
  • 65
  • 109
  • I did not say it was not legal. But it does not build. So no C# 6 was used during build, and this would fix it. – L-Four Oct 05 '16 at 13:23
  • Well public `bool IsReusable = false;` does not behave the same as `public bool IsReusable => false;`, you need to do `private readonly bool _isReusable = false; public bool IsReusable {get {return _isReusable; } }` – Scott Chamberlain Oct 05 '16 at 13:26
1

It seems that your coworker is using Visual Studio 2015 and you are using Visual Studio 2013 or earlier to compile the project.

If you don't want to upgrade your VS version you can replace the lambda expression with

public bool IsReusable { get { return false; } }

without any side effects.

haindl
  • 3,111
  • 2
  • 25
  • 31
  • This is true. He is using a community version of 2015 and I am using professional 2013. Thank you. – Keith Oct 05 '16 at 13:36
1

That line is defining a property that always returns false. It's functionally equivalent to public bool IsReusable {get {return false;} }.

The use of it when defining a property or method is called expression-bodied member. Since it's a new feature with C# 6.0, you have to make sure you are targeting 6.0 or it won't compile. VS2015 defaults to 6.0, but to use it in 2013 you have to install it separately. Your coworker has either already done this or is using VS2015.

Community
  • 1
  • 1
DrewJordan
  • 5,266
  • 1
  • 25
  • 39