Normally my properties look like this ("Version 1"):
public string Foo {get; set;}
I am fine with this. But sometimes my names get long, and it's not so good ("Version 2"):
public TypeWithALongName<AnotherLongNameInAngleBrackets> ThePropertyHasALongNameTooAndItsTooWideForTheScreen {get; set;}
When the name gets so long that the line isn't visible on the screen, it's not so good. I'd like to mitigate this by inserting a newline ("Version 3"):
public TypeWithALongName<AnotherLongNameInAngleBrackets>
ThePropertyHasALongNameTooAndItsTooWideForTheScreen {get; set;}
Now I can read it again! I like this. But VS does not. Format Document (control-K, control-D) now changes it to this ("Version 4"):
public TypeWithALongName<AnotherLongNameInAngleBrackets>
ThePropertyHasALongNameTooAndItsTooWideForTheScreen
{get; set;}
I want a way to ask VS to respect my newline choices in this area! If I use any of versions 1-4, Format Document should leave it alone. Is that possible?
If you want that behaviour. You can change the following setting.
Go to Tools->Options.
In the Options List. Navigate to Text Editor -> C# -> Code Style -> Formatting -> New Lines
Uncheck the box for "Place open brace for properties, indexers, and events".
This will give you the behaviour you want.
Checkout editorconfig if you want your styles to be shared with the dev team.
EditorConfig in VS 2022
Related
When I cut & paste the following C# text in VS 2022:
public int? Units { get; set; }
it pastes it formatted as:
public int? Units
{
get; set;
}
What .editorconfig rule needs to be adjusted to tell VS that I want my auto-implemented properties to remain on a single line?
Also, for heartfelt bonus points (but no actual points), is there a good way of finding this information myself? I googled this until my eyes bled, and I searched the options under Options -> Text Editor -> C# -> Code Style for as long as I could, with no luck.
In the .editorconfig UI, it is:
Whitespace -> Wrapping preferences -> Leave block on single line (checked)
In the .editorconfig file, add:
csharp_preserve_single_line_blocks = true
While this question is now answered, I still find myself frustrated trying to figure out how to find these options in these instances when I can't think of the right word or phrase to search by. To a certain extent it just requires luck, which does not make for a happy code monkey.
Is there a way to "Format document" in Visual Studio to insert braces around single-statement blocks for C# code? For example this:
if (x)
y();
... would become something like:
if (x) { y(); }
The auto formatting seems to deal with indentation but not this brace insertion. Is there a way to do it?
Actually, there seems to be something built-in to Visual Studio to do this.
If you go to Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines and make sure that you have Place open braces on new lines for control block checked.
Then, go to your document and use the key combination CTRLKD, this should reformat your document and add the curly braces.
In case you have resharper you can configure it to force braces depending on your criteria.
Than in the existing code press ctrlaltshiftf, it will format whole file. Or select just part of the code, in this case resharper will format just selection
P.S. ctrlaltf opens clean up dialog. You can configure cleanup options.
I gave a vote to Gimly's answer as it is pretty much correct. These things change over time of course. I would have added a comment but I wanted to paste in some images. Location of settings in VS2019 is at:
Uncheck the appropriate check boxes and select OK.
The shortcut key did not work, which is a shame because, I love them! This documentation suggests that you use
CTRL-K, CTRL-E or, you use the broom icon:
.
Neither option worked for me perhaps because, that is not the intention, given the list of options. However, if you mark and cut all your code and then, paste the code back into the file, the new standard is adopted.
I have a simple word add-on which I use to paste a sequence of strings into different places of a Microsoft Word document.
Currently I am using these lines of code, to get the information about the position:
int PageNumber = range.get_Information(WdInformation.wdActiveEndPageNumber);
int ColumnNumber = range.get_Information(WdInformation.wdFirstCharacterColumnNumber);
int LineNumber = range.get_Information(WdInformation.wdFirstCharacterLineNumber);
I need a way to track their place dynamically. Let’s say if user paste a name somewhere, and then our user decides to change the contents of document before this pasted name.
Do I need to parse the entire document to find my pasted string?
What if it’s a common string value like "Hello"? Can I hide or attach something to my pasted string dynamically? Like a pointer to specific string in document?
I appreciate any help or idea, thanks.
As per the comments above it sounds like bookmarks provide what you need. Bookmarks can be hidden - to do that (IIRC) you need to give them a name that begins with an underscore. If you want to iterate over your hidden bookmarks later on then you need to set
Bookmarks.ShowHidden = true
beforehand.
I am using Visual Studio 2008. In my editor whenever I write an automatic property it's "get" and "set" wraps to a single line whenever I wrote "set" and put semicolon(;). like this:
public string MyProperty
{
get; set;
}
I want it to be formatted like this
public string MyProperty
{
get;
set;
}
Currently I am manually formatting it to this after writing it. How can I set it as a default format?
Edit:
Options > Text Editor > C# > Formatting > Wrapping > Leave block on single line is already unchecked for me.
I unchecked all three option available in Options > Text Editor > C# > Formatting > General, but it doesn't work. Anything else?
If you put it all on one line, the default formatting options will leave it alone:
public string MyProperty { get; set; }
This is controlled by:
Options > Text Editor > C# > Formatting > Wrapping > Leave block on single line
If you really want to format it your way, you could disable:
Options > Text Editor > C# > Formatting > General > Automatically format completed block on }
But you'd probably want to disable Automatically format on paste too.
If you're using ReSharper, Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping has an option "Place abstract/auto property/indexer/event on single line" that controls the behavior you describe.
Tools -> Options -> Text editor has a lot of options for various languages as to how Visual Studio should (or should not) auto-format your code.
Look under Tools -> Options -> Text Editor -> C# -> Formatting.
You might find a setting there that will format it how you want.
EDIT
This is a workaround, but it would do the trick.
Make a code snippet for automagic properties. Here is link with more info on how to do that. It will need a little modification, but you can handle it. ;)
http://msmvps.com/blogs/kevinmcneish/archive/2007/04/30/property-code-snippet-for-visual-studio-2005.aspx
Do you have coderush or any other code generating addons installed?
Note - I believe this was https://github.com/dotnet/roslyn/issues/2837 and fixed in VS2015 Update 1.
I have a C# WinForms app with an About box. I am putting the version number in the about box using:
FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location)
.FileVersion
This ends up giving me the Subversion revision number from which the executable was built.
I would also like to get the date of the build into the About box. I tried:
File.GetLastWriteTime(Assembly.GetEntryAssembly().Location)
But that gives me the write date of the executable, which only corresponds to the date when the app was installed (we are using ClickOnce) not built.
How can I get the build date?
If you use automatic versioning, you can convert the last two bits of the version number into a build date: MSDN
We're using this very similiar piece of code:
DateTime buildDate = new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime;
and I'm pretty sure it doesn't change when installing from ClickOnce.. If I'm wrong please correct me!
You could change your assembly versioning to encode the date, but that would probably mean losing your subversion revision information which is arguably more useful.
This should work: write the current date/time into a .cs file as a pre-build task like so:
[assembly: AssemblyCreated(CreatedDate = new DateTime(...))]
You could use a batch file, PowerShell script or executable for that.
Include the file in your project (build action: compile) and include the custom attribute:
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class AssemblyCreatedAttribute : Attribute
{
public DateTime CreatedDate { get; set; }
}
On application start you can use reflection to get the custom attribute from the assembly for display in the about page.
The only way i was able to do it in C/C++ was to actually have a post-build process do a search and replace of a special string allocated as a "static const" in the binary.
There might be an easier way in c# though.
I don't think a standard way of doing it exists, but you can roll something up yourself. Create a custom assembly-level attribute, give it a cool name like 'AssemblyDateAttribute'. Let it take a string that you can parse into a DateTime in the constructor that is accessible via a property.
As part of a build process, create a new file with only the attribute being applied to the assembly. (Make it look like AssemblyInfo.cs) and then include that in your build input.
Then in your about box, search your assembly for instances of this attribute and display the date value in your box.
In Visual Studio projects there is a file AssemblyInfo.cs, but you can use any other .cs file. Look at the attribute AssemblyVersion:
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.*")]
Now you can calculate the build date by using the Version.Build property. The value that Version.Build returns is the number of days since: 2000/1/1
Assembly.GetExecutingAssembly().GetName().Version.ToString();
From my own C# code.