Visual Studio 2015 Property Formatting - c#

When I enter automatic properties C# properties in Visual Studio 2015 it insists of formatting them like this:
public int Age {get;set;}
I don't want the space after the "e" and before the "{", I want it to look like this:
public int Age{get;set;}
I didn't have this issue in VS2013, and even though I've exported my settings from VS2013 and imported them into VS2015 it still insists on inserting that space!
No matter what formatting options I select I can't seem to get it to do what I want. Does anyone know what crazy option combination I need to select to get it to stop putting the space in?

This automatic properties is part of the Visual Studio Code Snippet.
You should edit the default behavior for prop:
File:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Visual C#\prop.snippet
And change:
<Code Language="csharp"><![CDATA[public $type$ $property$ { get; set; }$end$]]>
To:
<Code Language="csharp"><![CDATA[public $type$ $property${ get; set; }$end$]]>

Related

No C# Code Analysis in Visual Studio Code

I'm currently using VS Code for C# development because I find it to be much more lightweight than Visual Studio itself, but there doesn't seem to be any code analysis tools available. This is a problem as I am not getting any warnings about unused functions (I am able to get warnings about unused local variables, however).
Some sample code:
namespace MyProject
{
// No warning for this unused class.
public class Dog
{
private int _age;
private string _name;
// No warning for this unused field.
private string _furColor;
public Dog(int age, string name)
{
// This unused variable generates a warning as expected.
int unusedVariable = 2;
_age = age;
_name = name;
}
// No warning for this unused private function.
private int Age()
{
return _age;
}
// No warning for this unused public function.
public string Name()
{
return _name;
}
}
}
I already have the official C# extension by Microsoft installed, but it doesn't seem to help. I can't find any code analysis extensions in the VS Code Marketplace either, and searching online only points me to extensions for Visual Studio. I am also aware that FxCop can be installed via NuGet, but it seems to only be for Visual Studio as well.
Are there any C# code analysis tools that can be used with VS Code? Even external tools or dotnet commands would help.
Special thanks to Martheen for suggesting this link.
All I had to do was add the following to my VS Code settings to enable better code analysis:
"omnisharp.enableRoslynAnalyzers": true // Enable C# code analysis.
This might be obvious, but I actually screwed it up:
You have to "enable" VS Code inside of your Unity Project
https://vionixstudio.com/2021/11/02/unity-visual-studio-code/
Open Unity Project
Edit --> Preferences --> (Analysis) External Tools
Change "External Script Editor" to VS Code
Click on "Regenerate project files"
If you click on a script this should open VS Code and the code completion should work (at least as long as everything else is set up, worked for me)

Visual Studio 2017 quick actions remove internal

I have a question about VS2017 quick actions. How can i remove the internal tag from the auto generated property with quick actions ?
Close your VS and navigate to
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC#\Snippets\1033\Visual C#
Look for a file named prop.snippet
Open the file with any text editor i.e. notepad
Look for the following line of code:
<Code Language="csharp"><![CDATA[public $type$ $property$ { get; internal set; }$end$]]>
and change it to:
<Code Language="csharp"><![CDATA[public $type$ $property$ { get; set; }$end$]]>
Please note this will effect every instance of VS. A better approach would be to create your own code snippet and import it in.
Please refer to Walkthrough: Create a code snippet for more info on creating custom code snippets.
In another way, you can use code snippets to create another template.

Refactoring "Implement Interface" using auto properties VS2015

I am trying to get Visual Studio 2015 (14.0) to use auto properties when implementing an interface using refactoring for C#.
I.e. I want this;
public object SomeProperty { get; set; }
as opposed to this;
public object SomeProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
I have accomplished this in past versions of Visual Studio by editing the code snippet file (instructions here) but I cannot get this to work using Visual Studio 2015.
Ok, so I stumbled upon the answer during my testing of VS2019 Preview (16.0).
In the main menu bar Tools --> Options --> Text Editor --> C# --> Advanced look for the option Implement Interface or Abstract Class under When generating properties choose prefer auto properties.
This results in the same outcome that snippets used to take care of pre VS2015.
You can solve by editing the PropertyStub.snippet
Just go to C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Refactoring open PropertyStub.snippet and edit:
$GetterAccessibility$ get
{
$end$throw new $Exception$();
}
$SetterAccessibility$ set
{
throw new $Exception$();
}
to
$GetterAccessibility$ get;
$SetterAccessibility$ set;

ReSharper 9 Adding menu item action not working

Trying to update my resharper extension to work for 9.0, before I was just moving the dll into the plugins directory but now I need to figure out how to get nuget to work... I've been able to package the files, dll gets included in the nupkg but I think I have some namespace\id something something issues(not very familiar with .net) and it doesn't seem as if my actions.xml is even being read by resharper when I import the nuget package. The menu item isn't being added. Anwyays if anyone can give me any sort of advice on how to debug a nuget package or what might be going wrong would really really appreciate as I've been stuck on this for a few days now.
Actions.xml
<?xml version="1.0" encoding="utf-8" ?>
<actions>
<action id="yuval" text="L10N"></action>
<insert group-id="ReSharper" position="last">
<action-ref id="yuval" text="About Localization Helper"/>
</insert>
</actions>
AboutAction.cs
namespace JetBrains.Resharper.L10N
{
[Action(Id)]
public class AboutAction : IExecutableAction
{
public const string Id = "yuval";
public bool Update(IDataContext context, ActionPresentation presentation, DelegateUpdate nextUpdate)
{
return true;
}
public void Execute(IDataContext context, DelegateExecute nextExecute)
{
MessageBox.ShowMessageBox(
"Localization Helper\nYuval\n\nHelps Localize",
"About Localization Helper",
MbButton.MB_OK,
MbIcon.MB_ICONASTERISK);
}
}
}
nuget spec
<?xml version="1.0"?>
<package >
<metadata>
<id>JetBrains.Resharper.L10N</id>
<version>1.0.0.7</version>
<title>L10N</title>
<authors>Yuval</authors>
<owners>UW</owners>
<licenseUrl>https://myurl.com</licenseUrl>
<projectUrl>https://myurl.com</projectUrl>
<iconUrl>https://myurl.com/logo.png</iconUrl>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<description>Tool to help localize</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2015</copyright>
<tags></tags>
<dependencies>
<dependency id="Wave" version="[1.0]" />
</dependencies>
</metadata>
<files>
<file src="..\bin\Debug\JetBrains.Resharper.L10N.dll"
target="dotFiles\"/>
</files>
</package>
The way actions are registered has changed in ReSharper 9. It's no longer done with actions.xml, but with interfaces on your action class. For example, to add an action to the ReSharper → Tools menu, you would do:
[Action(ActionId, Id = 1)]
public class AboutAction : IExecutableAction, IInsertLast<ToolsMenu>
{
public const string ActionId = "yuval";
// …
}
You also need to specify a unique value for Id. As of 9.1, this needs to be unique within your own extension (9.0 required it to be unique across the whole installation, including ReSharper itself and any other extensions).
Whenever you change the attributes or interfaces of an action, the extension needs to be reinstalled via nupkg (the actions are statically registered with Visual Studio, in the same way as a standard VS extension), but if just the implementation has changed, you can copy the dlls to the install folder, either manually, or via a small change to the .csproj.
You also need to make sure you've defined a ZoneMarker class. This declares that your action belongs to a zone, which is used to enable/disable functionality based on installed features and the current host (e.g. so Visual Studio specific extensions only work in VS and don't get loaded into dotPeek, etc.). You can find out more about Zones in the devguide, with this page providing useful info for defining a zone marker.
This thread should help, too.
Also, it's probably a good idea to name you dll and nupkg something other than JetBrains.ReSharper.(Whatever) to prevent any potential clashes with official dlls, and to prevent confusion as to where the dll comes from. The first part of the name is supposed to be your company's name (or personal name).

Set interface to public by default in C#

Is there any way for Visual Studio to create my interface 'public'? For example, right click on folder -> create new item -> code -> interface.
Whenever the file is created there are no access modifiers.
interface IMyInterface
{
}
Is there any way to default it to make it public?
public interface IMyInterface
{
}
I'm always forgetting to manually change them to public (when they need to be).
Open your project with the public IMyInterface interface shown above.
Go to File > Export Template.
Follow the steps (make sure you choose item template rather than Project template) and save it under a name of your choice.
When you restart visual studio and have a project open it will be available from the add item dialog. You will be able to set the name of the interface from the dialog just like you can with the other items.
Note that this does not overwrite the default interface template installed with Visual Studio so you can still just as easily make a private interface when required.
You can also create a code snippet for public interfaces like this :
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>public interface</Title>
</Header>
<Snippet>
<Code Language="csharp">
<![CDATA[
public interface IMyInterface
{
}
]]>
</Code>
</Snippet>
</CodeSnippet>
and save it as publicinterfacecsharp.snippet.
Then go to Tools -> Code Snippets Manager...
Select language as C# and My Code Snippets folder, then click Import.. and point to location where you saved the snippet. Now in any new project, you can right click in the editor window and select Insert Code Snippet -> My Code Snippets -> publicinterfacecsharp.

Categories

Resources