particular auto-format for Properties C# VS2010 - c#

I am using VS2010.
Auto formatting upon using '}' has been extremely useful for me, however recently it's been quite a pain while I've been working on a library.
If i use the line:
public Single ExampleProperty { get; set; }
It auto formats to:
public Single ExampleProperty
{
get;
set;
}
How can i stop this from happening? I don't want to disable auto-formatting, i just want to enable something like 'allow properties to be defined on a single line' or something.

Tools -> Options -> Text Editor -> C# -> Formatting -> Wrapping -> Leave block on single line
This should be what you are looking for

Related

How to Remove "nameOf in place of string" Error in VisualStudio

On loading the solution in VisualStudio, I get tons of error with "CA1507 Use nameof in place of string literal" error.
I can suppress it or add nameOf in my code but I dont want that. Also, It is occurring in more than 300 places in the code and only I am getting this error in my team.
All other devs in my team are not getting this error when they load the sln file.
Actual code :
[JsonProperty("code")] public string code { get; set; }
Error gets remove when I do this (but why I need to do this and no other devs in my team) :
[JsonProperty(nameof(code))] public string code { get; set; }
Other devs are saying some issue with how VS has loaded the solution. Also, I can't use nameOf because JSON properties don't match property names
Screenshot of Error
Firstly, if the letter case matches, then you don't need to add an attribute to the property.
// This is an unnecessary attribute, because the case of the word `code` is the same.
[JsonProperty("code")]
public string code { get; set; }
Just remove the attribute.
The attribute makes sense if the property casing is different from the field casing in json.
// code != Code - no warning
[JsonProperty("code")]
public string Code { get; set; }
You can always Suppress a warning.
The fact that you get an error message instead of a warning means that the TreatWarningsAsErrors compiler option is enabled in the project.
According to the documentation for the warning:
"To disable this entire category of rules, set the severity for the category to none in the configuration file."
EditorConfig
[*.{cs,vb}]
dotnet_diagnostic.CA1507.severity = none
why I need to do this and no other devs in my team
Check if you have a .editorconfig or .ruleset file on your machine that other devs don't have.
Adding an .editorconfig to version control that sets a specific severity will make the situation consistent across machines.

Is it possible to apply a VS2019 suggestion for an entire document

Is there a way to enact a Visual Studio suggestion (such as 'Fix Naming Violation') for an entire document or project? EDIT 2: I would like to do so for all class property names, which are all different in spelling.
When creating c# models for a json model that is known, you can use Paste Special to get the basic layout. However, since json is commonly camel case, the property names get pasted in the same way. The 'Fix Naming Violation' suggestion shows for each property, but I cannot seem to find a way to do so more globally.
I know you can change the naming violation rules, but was still curious to know if this is possible so as to follow C# convention.
I would post a screenshot but I don't have enough reputation.
EDIT 1:
From:
public string classProperty1 { get; set; }
public string classProperty2 { get; set; }
public string classProperty3 { get; set; }
To
public string ClassProperty1 { get; set; }
public string ClassProperty2 { get; set; }
public string ClassProperty3 { get; set; }
Step 1:
Just hover over and locate the suggestion or just hit Ctrl + . on where you want suggestions.
For eg:
Step 2:
Under the Preview Changes section, choose the scope of the suggested fix.
You get Document, Project and Solution.
For example, I want to apply this suggestion to the whole Solution.
Make selections if you'd like.
Step 3:
Click Apply.

Is there a way to instruct visual studio to auto add fields at the bottom of the class instead of the top?

namespace Guilds
{
public class Wizard
{
public void Wear(IClothing clothing)
{
Console.WriteLine("Puts on the {Robe} and {WizardHat}".Fmt(clothing));
}
IClothing _clothes;
IWeapon _weapon; // <== I want my fields added at the bottom of the class!
}
}
I am aware that if you put your fields at the bottom, it will start adding subsequent ones to the bottom of the class as well. I would love to have this as the default behavior even for the first field.
This behavior is usually triggered when pressing Ctrl + . on top of an undeclared field.
Use Regionerate and create your own format template. it's free tool to use with visual studio.
Edit: You can also use CodeMaid because it seems that Regionerate and VS2012 do not work together (I have not tested that combination at all though. I have VS2010)
Edit Adding more to my previous reply. CodeMaid is really cool and you can specify the layout in configuration. Also in configuration, you can specify that file should be formatted on save. This way write your code in anyway you want and have it formatted when you press Ctrl+S! I am one happy user of CodeMaid. Also I am using Visual Studio 2013.

Debugging automatic properties

Is there any way to set breakpoint on setter/getter in auto-implemented property?
int Counter { get; set; }
Other than changing it to standard property (I'm doing it in this way, but to do that I have to change and recompile whole project)
Using Visual Studio 2008, 2010, 2012, 2013:
Go to the Breakpoint window
New -> Break at Function…
For the get, type: ClassName.get_Counter()
For the set, type: ClassName.set_Counter(int)
You'll get a "No Source Available" when the breakpoint is hit, but you'll get the calling location in the call stack.
I found this solution here on MSDN
On Visual Studio 2017:
Hover over "set" word -> right click -> Breakpoint -> Insert Breakpoint
Before:
After:
This question is very old but it is worth noting that it just works in VS 2015.
https://devblogs.microsoft.com/devops/set-breakpoints-on-auto-implemented-properties-with-visual-studio-2015/
class X {
public string name {
set;
get; // setting a breakpoint here will break in VS 2015!
}
}
If I was you, I'd temporarily make the property a standard one backed by an internal field...set your breakpoints, and then you can change it back after.
Set Breakpoints where you are setting property or getting property, No other way.
you can do this by Find All References options
And Since it is only storing values and do not have any code in setter part so what do you debug?

Visual Studio/C# auto-format. Can I control newline after attributes

Visual studio keeps doing this:
[DataContract]
public class MyContract
{
[DataMember]
public bool MyBool { get; set; }
[DataMember]
public string MyString { get; set; }
}
I would like this:
[DataContract]
public class MyContract
{
[DataMember] public bool MyBool { get; set; }
[DataMember] public string MyString { get; set; }
}
No big deal if the 'public class MyContract' is on the same line as the [DataContract].
Visual studio seems to have a lot of detailed autoformatting options, but I can't find any regarding newlines after attributes. Am I missing something here? Or is it just not available.
EDIT: At very least, I'd like a "don't change what I entered" formatting option, as opposed to a "always insert" or "always remove" newline option. It's super-annoying that it keeps unformatting my code after I type.
What I usually do is hit Ctrl-Z the very moment autoformat jumps in where I don't appreciate it.
I.e., on a closing accolade, which formats a whole class or method. Type the closing accolade, see it changing the way you don't like it and then hit Ctrl-Z. (I know you were looking for an actual option, I don't know if any exists).
Not sure if it works for attributes, but look under Tools -> Options -> Text Editor -> C# -> Formatting -> Wrapping -> Leave block on single line or Leave statements and member declarations on the same line.
ReSharper can do that. It has options for:
Place type attribute on same line
Place method attribute on same line
Place property/indexer/event attribute on same line (this is the one you want)
Place field attribute on same line
It costs a few bucks but if you're as obsessive as I am it's worth every penny. ;)
I also remap Ctrl+K, Ctrl+D to ReSharper's silent format code to experience formatting bliss.
What worked best for me was to disable auto format on ; and paste. I hardly ever want auto format at these times anyways. This allows you to type out attributes and move them around without pesky interference.
Options > Text Editor > C# > Formatting > General
It is ReSharper. Extensions/ReSharper/Options/Code Editiong/C#/Formatting Style/ Line Breakes and Wrapping:
Line Breaks and Wrapping:
Yeah, Ctrl+E, D is your friend. You can optimize the formatting in Text editor options

Categories