resharper

Advantages of using const instead of variables inside methods

微笑、不失礼 提交于 2019-11-26 15:50:23
问题 Whenever I have local variables in a method, ReSharper suggests to convert them to constants: // instead of this: var s = "some string"; var flags = BindingFlags.Public | BindingFlags.Instance; // ReSharper suggest to use this: const string s = "some string"; const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; Given that these are really constant values (and not variables) I understand that ReSharper suggest to change them to const. But apart from that, is there any other

LINQ: Not Any vs All Don't

╄→尐↘猪︶ㄣ 提交于 2019-11-26 15:48:47
Often I want to check if a provided value matches one in a list (e.g. when validating): if (!acceptedValues.Any(v => v == someValue)) { // exception logic } Recently, I've noticed ReSharper asking me to simplify these queries to: if (acceptedValues.All(v => v != someValue)) { // exception logic } Obviously, this is logically identical, perhaps slightly more readable (if you've done a lot of mathematics), my question is: does this result in a performance hit? It feels like it should (i.e. .Any() sounds like it short-circuits, whereas .All() sounds like it does not), but I have nothing to

Visual Studio or Resharper functionality for placement of using directives

雨燕双飞 提交于 2019-11-26 15:39:10
问题 I like to put my using directives inside the current namespace, and not outside as VS and Resharper per default puts them. Does anyone know of a macro/standard functionality that sorts/removes unused using directives and puts them inside the current namespace? 回答1: UPDATE - ReSharper 2016.1 : This option is now moved to Code Editing → C# → Code Style → Add 'using' directive to the deepest scope Have you tried the ReSharper option: Languages → C# → Formatting Style → Namespace Imports → Add

Resharper's example code for explaining “Possible multiple enumeration of IEnumerable”

北慕城南 提交于 2019-11-26 15:36:51
问题 Sometimes Resharper warns about: Possible multiple enumeration of IEnumerable There's an SO question on how to handle this issue, and the ReSharper site also explains things here. It has some sample code that tells you to do this instead: IEnumerable<string> names = GetNames().ToList(); My question is about this specific suggestion: won't this still result in enumerating through the collection twice in the 2 for-each loops? 回答1: GetNames() returns an IEnumerable . So if you store that result:

ReSharper - Possible Null Assignment when using Microsoft.Contracts

依然范特西╮ 提交于 2019-11-26 15:08:28
问题 Is there any way to indicate to ReSharper that a null reference won't occur because of Design-by-Contract Requires checking? For example, the following code will raise the warning ( Possible 'null' assignment to entity marked with 'NotNull' attribute ) in ReSharper on lines 7 and 8: private Dictionary<string, string> _Lookup = new Dictionary<string, string>(); public void Foo(string s) { Contract.Requires(!String.IsNullOrEmpty(s)); if (_Lookup.ContainsKey(s)) _Lookup.Remove(s); } What is

Can Resharper be set to warn if IDisposable not handled correctly?

半世苍凉 提交于 2019-11-26 14:26:21
问题 Is there a setting in Resharper 4 (or even Visual Studio itself...) that forces a warning if I forget to wrap code in a using block, or ommit the proper Dispose call in a finally block? 回答1: Correct automatic Dispose analysis requires DFA (Data Flow Analysis) in a global way. It is unlikely that you create an IDisposable object and doesn't call any method on it and do not pass it around as an argument. If disposable object is passed to other methods (including calling its members, when "this"

Resharper runs UnitTest from different location

允我心安 提交于 2019-11-26 14:18:33
问题 When I run unit tests with Visual Studio it works fine, because it runs from project directory where all assemblies are. But when I run it with resharper it goes with error on var services = Assembly.Load("SomeAssembly"); with error Could not load file or assembly 'SomeAssembly' or one of its dependencies. The system cannot find the file specified.. So i've tried var path = Assembly.GetExecutingAssembly().Location; and it's not project one. It's C:\Users\*UserName*\AppData\Local\Temp

What does CultureInfo.InvariantCulture mean?

。_饼干妹妹 提交于 2019-11-26 14:09:31
I have a string of text like so: var foo = "FooBar"; I want to declare a second string called bar and make this equal to first and fourth character of my first foo , so I do this like so: var bar = foo[0].ToString() + foo[3].ToString(); This works as expected, but ReSharper is advising me to put Culture.InvariantCulture inside my brackets, so this line ends up like so: var bar = foo[0].ToString(CultureInfo.InvariantCulture) + foo[3].ToString(CultureInfo.InvariantCulture); What does this mean, and will it affect how my program runs? JohnB Not all cultures use the same format for dates and

Resharper: vars

…衆ロ難τιáo~ 提交于 2019-11-26 13:58:39
问题 Why does Resharper want you to change most variables to var type instead of the actual type in the code? 回答1: It's just an option. You can disable it: ReSharper -> Options -> Code Inspection -> Inspection Severity -> Code Redundencies -> Use 'var' keyword where possible: change this to "Do not show" There's also the context (lightbulb) option which will take you in each direction - this is under ReSharper -> Options -> Languages -> C# -> Context Actions -> "Replaces explicit type declaration

Is Int32.ToString() culture-specific?

北城以北 提交于 2019-11-26 12:46:03
问题 I\'m running a beta version of ReSharper, and it\'s giving me warnings for the following code: int id; // ... DoSomethingWith(id.ToString()); The warning is on the id.ToString() call, and it\'s telling me \"Specify a culture in string conversion explicitly\". I understand the warning, and I know how to fix it -- just change the code to the much more unwieldy id.ToString(CultureInfo.InvariantCulture) . But my question is: is that necessary? I mean, obviously it\'s important to specify the