Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolate string c# 6.0 and Stylecop

Tags:

I am using Stylecop version : 4.7.49.0

Has anyone used the latest interpolate string functionality in c# 6.0

example

var totalUnits = GetUnitsGetTotalIssuedShares(myId);
var testString = $"Test Units :{totalUnits}, have been shipped.";

When I build i get the stylecop error SA0102 - because stylecop cant parse the file. It doesn't seem like there is a new version of stylecop that can handle 6.0 yet?

error :SA0102: A syntax error has been discovered in file

Is there anyway around this error?

like image 629
MicroMan Avatar asked Jul 23 '15 20:07

MicroMan


People also ask

What is string interpolation in C?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

What is string interpolation method?

String interpolation is a technique that enables you to insert expression values into literal strings. It is also known as variable substitution, variable interpolation, or variable expansion. It is a process of evaluating string literals containing one or more placeholders that get replaced by corresponding values.

What is the difference between string interpolation and concatenation?

You can think of string concatenation as gluing strings together. And, you can think of string interpolation without strings as injecting strings inside of other strings.

Why do we use string interpolation in C#?

C# string interpolation is used to format and manipulate strings. This feature was introduced in C# 6. Using string interpolation, we can use objects and expressions as a part of the string interpolation operation. C# string interpolation is a method of concatenating, formatting and manipulating strings.


1 Answers

SA0102 is an internal stylecop error so can't be supressed or ignored via a settings file.

You can suppress a file from stylecop by finding the filename and changing the section to this in the csproj:

<Compile Include="<filename>.cs">
  <ExcludeFromStyleCop>True</ExcludeFromStyleCop>
</Compile>

You can get the same effect by right clicking on the offending file and selecting "exclude from style cop" if you have the StyleCop plugin installed. It currently needs to be 4.7.50 alpha for Visual Studio 2015.


The more modern way of doing this is to make use of the Analyzers feature of Visual Studio 2015, with StyleCop.Analyzers.

enter image description here

Moving to Stylecop.Analyzers, this would add them into the rules in the relevant *.ruleset file (same place as CodeAnalysis rules)

enter image description here

and you can run them via

enter image description here

Which has the same effect as right click Run StyleCop:

I.e. giving:

enter image description here

This will have better support for C# 6, as StyleCop 4.7.50, which Supports Visual Studio 2015, is in alpha and does not yet support C# 6.

like image 137
NikolaiDante Avatar answered Sep 28 '22 05:09

NikolaiDante