Visual studio как отключить ошибки

Search code, repositories, users, issues, pull requests…

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

To suppress warnings for all projects, you need to create a .editorconfig file in a top-level directory. For example, I have mine in trunk and I commit it to source control so that my colleagues share the same settings.

The settings in this file apply to all projects in trunk and subfolders, unless overridden by another .editorconfig file further down the folder tree e.g. you might you have a project specific EditorConfig file in a subfolder which has different settings. See File hierarchy and precedence for more details.

Creating an EditorConfig file

You can use a text editor for this if you just want to change one specific setting. However, Visual Studio can create a .editorconfig file with sensible defaults for .NET for you. From MSDN:

  • Create a new project

  • From the menu bar, choose Project > Add New Item; or press Ctrl+Shift+A

  • Select the editorconfig File (.NET) template to add an EditorConfig file prepopulated with default .NET code style, formatting, and naming conventions

enter image description here

  • Optionally delete the project — we don’t really need it

Visual Studio 2019 — Creating an EditorConfig file from current settings

In Visual Studio 2019, you can instead create an EditorConfig file from your current settings. Just click the following button in the Options dialog under Text Editor > C# > Code Style > General:

enter image description here

If you’re creating in a text editor you’ll probably need this at the top of the file, adjusted as necessary:

# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true

# C# files
[*.cs]

Disabling IDE0044 in the editor config file

To disable IDE0044 specifically, add or change the following setting in the .editorconfig file:

dotnet_style_readonly_field = false:none

(In Visual Studio 2019, you can set the Prefer readonly option to No under TextEditor-> C# -> Code Style-> General in Options and then press the Generate .editorconfig file from settings button as detailed above).

In debug mode, if there is an error in our application when we press F5 to start debugging, a dialog appears with the warning: «Your program has an error. Do you want to run your last modified program?» or something like this.

I want to enable or disable this dialog.

How can I do this?

Benjol's user avatar

Benjol

64.1k54 gold badges187 silver badges268 bronze badges

asked Jan 17, 2011 at 10:20

Tavousi's user avatar

3

You can turn that prompt on/off in your Visual Studio settings:

  1. From the «Tools» menu, select «Options».
  2. In the dialog that appears, expand «Projects and Solutions», and click «Build and Run».
  3. On the right side, you’ll see a combo box labeled «On Run, when build or deployment errors occur».

    • If you want to disable the message box, select either «Do not launch» or «Launch old version» (which will launch the old version automatically).
    • If you want to enable the message box, select «Prompt to launch» which will ask you each time.

   VS "Build and Run" Options

Of course, as people have suggested in the comments, this means that your code has errors in it somewhere that are preventing it from compiling. You need to use the «Error List» to figure out what those errors are, and then fix them.

answered Jan 17, 2011 at 10:29

Cody Gray - on strike's user avatar

Code Focused

Hide Compiler Warnings in Your Spare Time

Learn how to keep new code-style warnings from filling up your formerly pristine Visual Studio Error List panel.

As Microsoft .NET Framework languages have grown in functionality, they’ve started tossing out compiler warnings like your development life depended on it. Messages about unused variables can sometimes help locate esoteric bugs. But new code-style warnings, such as whether your variables begin with capital letters, can easily fill up the formerly pristine Visual Studio Error List panel.

Of course, it’s always best to address the more meaningful warnings as quickly as possible. But if there are situations where you need to keep the questionable code as it is, and you don’t want to see the annoying warning message hanging around, Visual Studio offers a few different ways to suppress these messages, on scales from a single source line up to your entire project.

Consider, as an example, an asynchronous method that lacks an embedded await statement:

private async Task DoSomeWork()
{
  // ----- This method lacks an await statement, and therefore
  //       generates C# compiler warning CS1998.
}

Such C# code will add warning CS1998 to your Visual Studio experience, as shown in Figure 1.

[Click on image for larger view.]
Figure 1. Missing Await Statement Warning

The Code column in the Error List panel identifies the specific warning identity. If you want to hide a particular warning code across your entire project, open the overall properties through the Project | Properties menu command, and when the properties appear, select the Build page. The Suppress Warnings box on that page accepts a semicolon-delimited list of codes. Entering CS1998 in that field (see Figure 2) prevents the compiler from reporting that warning project-wide.

[Click on image for larger view.]
Figure 2. Project-Level Warning Suppression

If your warning-suppression needs are a bit more targeted, use the C# language #pragma statement to hide messages within a code file or block of source. The «warning disable» version of the statement, when paired with a comma-delimited list of codes, disables those codes for the remainder of that file’s processing:

#pragma warning disable CS1998

Leave off the list of codes to disable all warnings. If you want to reactivate the warnings later in the file, use the «warning restore» version of the statement instead. When used together, the two variations of the #pragma statement let you limit warning hiding to specific code lines:

#pragma warning disable CS1998 // Warning suppressed from here
private async Task DoSomeWork()
#pragma warning restore CS1998 // Warning recognized from here
{
}

If typing these statements is more than your fingers are willing to do, right-click on the warning message in the Error List panel and choose the Suppress | In Source command from the shortcut menu that appears. This action will surround the warning-laden line with the relevant #pragma statements.

Yet another way to disable warnings involves the SuppressMessage attribute, found in the System.Diagnostics.CodeAnalysis namespace. When applied to a method or other relevant code element, the attribute deactivates the indicated warning in a more object-centric manner:

// ----- Assumes: using System.Diagnostics.CodeAnalysis;
[SuppressMessage("Compiler", "CS1998")]
private async Task DoSomeWork()
{
}

I determined the first argument passed into the SuppressMessage attribute, «Compiler,» by turning on the Category column in the Error List panel and copying the text that appears for that warning. There are other options included with the attribute that let you narrow down the focus to specific code features. Microsoft recommends that you use SuppressMessage sparingly. Whereas #pragma does its work early in the compilation process and then says goodbye, the suppression attribute adds content to your final assembly, leading to attribute bloat or worse.

The MSDN documentation (such as this for C#) lists many of the error and warning codes you might encounter, allowing you to be proactive in your warning-suppression activities. But because one major goal of programming is to fix problems instead of hiding them, it’s probably best to act on individual messages as they pop up in the Error List panel. Let this be a warning to you.

About the Author


Tim Patrick has spent more than thirty years as a software architect and developer. His two most recent books on .NET development — Start-to-Finish Visual C# 2015, and Start-to-Finish Visual Basic 2015 — are available from http://owanipress.com. He blogs regularly at http://wellreadman.com.

Skip to content

We all have to get zero errors when compiling solutions, but we all aim for zero warnings, too, right?

Supressing Code Analysis errors is easy enough – and I recommend using code analysis rules (though you don’t need to enforce ALL of them. Consider what rules are relevant.)

However – what do you do if you have errors like this, it can be a problem:

6>  blah\Caching\TokenCacheItem.cs(39,48,39,65): warning CS0067: The event ‘TokenCacheItem.DataLengthChanged’ is never used

The thing is, I need DataLengthChanged to fulfil an interface. But yes, it isn’t used. Oh, the conundrum.

What I hadn’t realised was you can disable compiler warnings (steady – careful!). See: https://stackoverflow.com/questions/3820985/suppressing-is-never-used-and-is-never-assigned-to-warnings-in-c-sharp/3821035#answer-3821035

We’re instructing the compiler to disable the warning for rule 0067, which is the error given above, and then immediately after the problematic line, we’re re-enabling it.

And as noted on the post…

Caveat: As per the comment by @Jon Hanna, perhaps a few warnings is in order for this, for future finders of this question and answer.

  • First, and foremost, the act of suppressing a warning is akin to swallowing pills for headache. Sure, it might be the right thing to do sometimes, but it’s not a catch-all solution. Sometimes, a headache is a real symptom that you shouldn’t mask, same with warnings. It is always best to try to treat the warnings by fixing their cause, instead of just blindly removing them from the build output.

  • Having said that, if you need to suppress a warning, follow the pattern I laid out above. The first code line, #pragma warning disable XYZK, disables the warning for the rest of that file, or at least until a corresponding #pragma warning restore XYZKis found. Minimize the number of lines you disable these warnings on. The pattern above disables the warning for just one line.

  • Also, as Jon mentions, a comment as to why you’re doing this is a good idea. Disabling a warning is definitely a code-smell when done without cause, and a comment will prevent future maintainers from spending time either wondering why you did it, or even by removing it and trying to fix the warnings.

Roll on zero-warning compilations.

Понравилась статья? Поделить с друзьями:
  • Visual studio scanf ошибка
  • Vipnet csp ошибка 1603
  • Visual studio произошла неизвестная ошибка при установке
  • Visual studio just in time debugger ошибка
  • Visual studio показывает несуществующие ошибки