Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Visual Studio 2017's "Implement Interface" command generate C# 5 code?

In Visual Studio 2017, in the "Quick Actions and Refactorings" menu, when I click "Implement Interface", I get implementations that use newer features of C#, like this:

public int ConnectionTimeout => throw new NotImplementedException();

The problem is that my company is using C# 5 and so we can't use this newer syntax. I know older versions of Visual Studio would generate this code instead:

public int ConnectionTimeout { get { throw new NotImplementedException(); } }

Is there a way to get Visual Studio 2017 to do this? I've set my project to compile using C# 5.

Edit: I noticed that the code generator is inconsistent. If the interface has a bool member that only has a getter, it uses the old syntax. Otherwise it uses the new syntax. This makes me think that I'm out of luck.

private interface myint
{
    bool bool1 { get; }
    bool bool2 { get; set; }
    bool bool3 { set; }

    int int1 { get; }
    int int2 { get; set; }
    int int3 { set; }

    string string1 { get; }
    string string2 { get; set; }
    string string3 { set; }
}

private class myclass : myint //I clicked "Implement Interface" on this
{
    public bool bool1
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public bool bool2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public bool bool3 { set => throw new NotImplementedException(); }

    public int int1 => throw new NotImplementedException();

    public int int2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public int int3 { set => throw new NotImplementedException(); }

    public string string1 => throw new NotImplementedException();

    public string string2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public string string3 { set => throw new NotImplementedException(); }
}
like image 314
user2023861 Avatar asked Apr 04 '17 14:04

user2023861


People also ask

How do you implement an interface in c#?

On implementation of an interface, you must override all of its methods. Interfaces can contain properties and methods, but not fields/variables. Interface members are by default abstract and public. An interface cannot contain a constructor (as it cannot be used to create objects)

How is interface implemented in VS code?

UPDATE: VS Code now supports this functionality out of the box. Just add the interface, the compiler will highlight the controller with red, right click on the light bulb on the left and click implement interface. Enjoy!


1 Answers

Go to Tools -> Options -> Text Editor -> Code Style -> General, scroll to Code block preferences section and change the preference For properties, For indexers and For accessors from "Prefer expression block" (default) to "Prefer block body".

enter image description here

like image 109
Ivan Stoev Avatar answered Nov 16 '22 01:11

Ivan Stoev