-1

Writing a simple class in c# comes up with error "Not all code path return a value"

public class Genre
{
    private string _name;

    public string Name
    {
        get => _name;
        set => _name = value;
    }
}
Fuzzybear
  • 1,388
  • 2
  • 25
  • 42
AnKing
  • 1,994
  • 6
  • 31
  • 54
  • 1
    Which line? (compiles fine for me) This error is usually generated when a non-void function misses a return statement – Vladi Pavelka Jul 11 '18 at 15:21
  • 2
    Compiles without error for me in LINQPad, with a `new Genre().Name = "hi";` thrown in for good measure. What's the actual failing code? – Jeroen Mostert Jul 11 '18 at 15:21
  • I'm sure this class doesn't create a error – Antoine V Jul 11 '18 at 15:21
  • 1
    In a side note, you can use the snippet propfull so you can write code faster and without errors - https://msdn.microsoft.com/en-us/library/z41h7fat.aspx – Afonso Jul 11 '18 at 15:23
  • Which .net framework you are working on? – huMpty duMpty Jul 11 '18 at 15:23
  • 1
    4.6.2 framework – AnKing Jul 11 '18 at 15:23
  • This is a new-ish syntax, so check which version of the C# compiler you're using. You'll need C# 6.0 or above, I think. Note that this is not the same as the .NET framework version. See https://davefancher.com/2014/08/25/c-6-0-expression-bodied-members/ and other online resources – ADyson Jul 11 '18 at 15:24
  • where do i check the compiler version? – AnKing Jul 11 '18 at 15:25
  • see https://stackoverflow.com/a/49647107/5947043 (and many others again, if you take the time to google something like "check c# compiler version"). Google is your friend... – ADyson Jul 11 '18 at 15:27
  • @ADyson I think it's C# 7, which means you must have VS 2017 or above. AnKing, what's your VS version? https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#more-expression-bodied-members – Magnetron Jul 11 '18 at 15:28
  • @AnKing: Open project in visual studio right click on project -> properties->build-> advance and check the language version dropdown... see what c# versions it show – huMpty duMpty Jul 11 '18 at 15:28
  • I use VS2015, changed compiler version to 6, but still doesnt work. do I really need VS2017 for this syntax to work? – AnKing Jul 11 '18 at 15:29
  • @AnKing As I said, you must have VS2017, otherwise you'll have to use the old way – Magnetron Jul 11 '18 at 15:30
  • @Magnetron Good spot. It's C#6 for property "get" using this syntax, and C# 7 to use property "set" this way. See https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members for that. But also see https://stackoverflow.com/questions/39461407/how-to-use-c7-with-visual-studio-2015 - you can install different compilers into a particular project when using Nuget on VS 2015 and possibly earlier, although it's not clear if this is really supported or not. Of course a command-line compiler could always be used, instead. – ADyson Jul 11 '18 at 15:31
  • @ADyson good to know. But as the post says, it'll work but will show sintax error. – Magnetron Jul 11 '18 at 15:34
  • Might seem ridicolous, but make sure that you are compiling the right project. – Axel2D Jul 11 '18 at 15:37
  • I think I'll just have to upgrade the visual studio at this point – AnKing Jul 11 '18 at 15:38

1 Answers1

2

This syntax is a new addition to C#7, so the options are:

  • Use Visual Studio 2017 or above, or VS Code (if dotnet core project)
  • Install the new compiler from Nuget, check this answer on how to do so
  • Use the old way:

    public class Genre
    {
        private string _name;
    
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
    
Hardik
  • 3,038
  • 2
  • 19
  • 31
Magnetron
  • 7,495
  • 1
  • 25
  • 41