How do I disable namespace abbreviation in Visual Studio 2015? - c#

If I'm not mistaken, since Visual Studio 2015 there's some new feature that grays out redundant parts of namespace usings. Also, when you automatically add an using to some assembly member using quick actions, the grayed part is omitted.
For example, the whole Whatever.Framework.Shared.Data.Mongo would be added as just Shared.Data.Mongo when using quick actions.
Is it possible to completely disable this refactoring feature?

You will see this even going back to Visual Studio 2012 (and possibly older) under the following circumstances:
namespace Test.Foo {
using Test.Foo.Bar; // can write using Bar
public class Class1 {
}
}
vs
using Test.Foo.Bar;
namespace Test.Foo {
public class Class1 {
}
}
The first will grey out the Test.Foo. part in the using statement, as you are inside the namespace declaration. The second will not.

Related

How can I use a file-scoped namespace declaration in a class template?

C# 10 introduced file-scoped namespaces, which I would like to use in Visual Studio's class templates. I've updated the 'Class' template file to the following:
namespace $rootnamespace$;
class $safeitemrootname$
{
//I put this comment here to make sure it's using the right file
}
But when I create a new empty class I get this autogenerated code:
namespace ProjectName
{
internal class Class1
{
//I put this comment here to make sure it's using the right file
}
}
What do I need to do to make the auto-generated code for an empty class look like this?
namespace ProjectName;
internal class Class1
{
}
For reference, I am using Visual Studio 2022 Professional and my project is using C#10 with .NET 6.
The location of the class template file that I am modifying is: C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
You have to set up your project's editorconfig to prefer File-scoped namespaces.
Right click your project. Select "Add" → "New Item"
Select "editorConfig File (.NET)"
Double click the new editorconfig file. In the "Code Style" tab set "Namespace declarations" to "File scoped"
The code template will now work as expected.
Check this thread: https://stackoverflow.com/a/69889803
They use a .editorconfig file where you can specify the namespace declaration style. When creating a new file in VS 2022 it will use that new style

Avoid auto generated namespace in Visual Studio. How to turn off automatic insertion of "using System;"

I am using VS Community 2017 Version 15.2
When I use the light-bulb suggestions, or manually press (Ctrl + .), it generates a method like this:
using System;
public void f() {
throw new NotImplementedException();
}
I want it to be this way:
public void f() {
throw new System.NotImplementedException();
}
Without the:
using System;
I looked through the settings and also online but could not find the option.
I want this because I like keeping my namespace clean. I don't want to manually delete the using statement every time I use this handy feature.
It is not a common behaviour, and Visual studio does not provide it. If you use Resharper, you can set it manually.
Resharper -> Option
(in option window) Code Editing -> C# -> Code Style
Find Reference qualification and check prefer fully qualified references

Detect c# version at compile time

I have an old line of c# code that looks basically like this:
foo.set_Parent(parent);
It has compiled fine for years. Now in VS2015 I get the error:
CS0571 'Foo.Parent.set': cannot explicitly call operator or accessor
So I can rewrite the line as:
foo.Parent=parent;
This builds fine in VS2015, but in VS2013 it gives the error:
'Foo.Parent' is not supported by the language; try directly calling
accessor methods 'Foo.get_Parent()' or Foo.set_Parent(Foo)'
So the simple fix is to simply ifdef these two lines based upon which version of the compiler is running. But how do you detect which version of the compiler is executing?
And for the record, no, I can't just dictate that everyone on the team simultaneously upgrades to VS2015.
Additional info -
For everyone smelling a rat, I'll go ahead and drag out the ugly truth, although I don't think it will change much of anything. The class Foo is from an ancient Borland assembly that is all bound up in Delphi (and yes, we're migrating away but not there yet). So the actual code, that compiles up to VS2013, looks like this:
using Borland.Vcl;
using RepGen;
using SnapReportsForm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace MigrantCOM {
[ComVisible(true)]
[Guid("48245BA3-736B-4F98-BDC5-AD86F77E39F4")]
[ProgId("MigrantCOM.Exports")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MigrantCLRExports { // : MarshalByRefObject
public string Test(string s) { return s+s; }
}
[ComVisible(true)]
[Guid("1154D364-B588-4C31-88B9-141072303117")]
[ProgId("MigrantCOM.SnapRepCOM")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class SnapRepCOM {
TRepGen repGen;
TStringList snapRefs=new TStringList();
TForm parent=new TForm(null);
TMemo designerMemo;
List<TReference> references=new List<TReference>();
TRunAsSnapContext runAsSnapContext=new TRunAsSnapContext();
public SnapRepCOM() {
designerMemo=new TMemo(parent); designerMemo.set_Parent(parent);
...
}
So the class being instantiated is Borland.Vcl.TMemo which is part of the old Delphi assembly.
I'm leaving this as an answer, linking an image will fit better here than in a comment.
So if you want to use VS 2015 but still use the same good ol' version of the C# language that worked for years, you can configure your project to target a specific version:
This adds <LangVersion>5</LangVersion> in the csproj.

How can I change the default Visual Studio C# new class file template? [duplicate]

This question already has answers here:
How do you default a new class to public when creating it in Visual Studio?
(9 answers)
Closed 9 years ago.
Is it possible to change the template in Visual Studio 2010 so that the class definition is changed from:
class Class1
{
}
to:
public class Class1
{
}
When creating a new class via Add->Class in the context menu.
I would also ideally like to be able to create a class in one context menu click. I copy+paste existing class files to avoid the file dialog.
You could modify the following file:
c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
It contains the template used when you add a new class. Inside the same folder you also have the template for interfaces: Interface.zip so that they are public by default. IIRC a restart of VS is necessary to pick the changes.
You can create your own template by putting a file in C:\Users\you\Documents\Visual Studio 2010\Templates\ItemTemplates\Visual C#.
For example, you can put "publicclass.cs" with this content :
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
{
public class $safeitemrootname$
{
}
}
In order to avoid the class dialog, you can use the smart tag. Anywhere you would to use an inexisting class, simply type the class name, and press AltShiftF10 to popout the "generate class" menu.
You have to manually edit the template files of Visual Studio.
See this link for a detailed HOW-TO.
This is possible as described here and here.
You might see some issues due to the Template Cache of VS - on how to deal with them see esp. the comments here.
An "official" source on how to do this can be found at http://blogs.msdn.com/b/oanapl/archive/2009/03/06/visual-studio-templates-add-new-item-to-project.aspx

Refactoring/renaming within comments in Visual C# Express

When refactor->renaming MyMethod in the following example in Visual C# 2010 Express, the method name 'MyMethod' in the comment won't be changed.
Usually that a comment isn't touched by refactor/rename is for good reasons, since how should the IDE know that it's not just some word but actually references the very method that it is refactoring.
So is there a way in which a word in a comment can be bound to a class/method/variable name within Visual C# 2010 Express, so that it's changed as well during refactoring/renaming?
If there are several ways what would be the easiest/cheapest way?
(cheapest - in case of plugins or commercial versions)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// text for class MyClass
public class MyClass
{
/// <summary><c>MyMethod</c> is a method in the <c>MyClass</c> class.
/// </summary>
public static void MyMethod(int Int1)
{
}
/// text for Main
public static void Main()
{
Console.ReadLine();
}
}
}
At least in the commercial editions, the rename dialog (press F2) has an Search in Comments checkbox.
Not Sure about the Express edition, but VS 2008 professional edition we can do it:
http://www.thereforesystems.com/refactoring-in-visual-studio-2008/
According to wikipedia, the Renaming refactoring mentioned in other answers should be available in the Express edition. So try Ctrl-R-R, F2 or right-click/Refactor/Rename.

Categories