I am attempting to complete leetcode's problem #1266 (https://leetcode.com/problems/minimum-time-visiting-all-points/).
My solution works on Visual Studio just fine, but when I submit it to leetcode I encounter a compile time error :
Line 19: Char 12: error CS8320: Feature 'tuple equality' is not
available in C# 7.2. Please use language version 7.3 or greater. (in
Solution.cs)
(int, int) current = test[0];
(int, int) target = test.Last();
for (int i = 1; i < test.Count; i++)
{
target = test[i];
while (current != target) <<<<<<<<<<<<< ERROR LINE according to leetcode
I believe my VS2019 was up to date to begin with (I am now updating to the latest build : 16.3.9 --> 16.4.0) so I'm not sure what else I need to do on my end.
My question is : is there a simple alternative I could use or should I just move on? (As far as I'm concerned my solution passes - it completed the given test cases.)
Thanks for any time and help.
P.S. Sorry if you need more of my code, I'll include it if asked for. It's rather hacky and long, haha.
Instead of comparing tuples, you can compare tuples' properties:
...
// tuples current and target are not equal if either Item1 or Item2 are not equal
while (current.Item1 != target.Item1 || current.Item2 != target.Item2)
...
You may want explicit Tuple<int, int> syntax as well:
Tuple<int, int> current = test[0];
Tuple<int, int> target = test.Last();
Recently I came across a code base and found some code like below
var a = 1_23_456;
Console.WriteLine(a);
I have tried to run it in visual studio 2015/ .net fiddle but it got a compilation error. But when I retried it using Roslyn 2.0 compiler, it got compiled and gives me the output 123456.
What the matter here? Why it is showing the data as an integer ?
The underscores are the digit separator. They're used to make it easier to read large numbers (particularly binary numbers). You can read about them on MSDN.
The underscores don't change the datatype. All of the following statements result in the same data type (int or System.Int32) and value:
var a = 123456;
int b = 123456;
System.Int32 c = 123456;
var d = 1_23_456;
int e = 1_23_456;
System.Int32 f = 1_23_456;
You will need the new compiler in Visual Studio 2017 to compile it, though you may be able to get away with using Visual Studio 2015.
After installing Visual Studio 2015 and building my project I receive the error
"CSC : error CS7038: Failed to emit module".
However my solution is building fine in Visual Studio 2013.
It is an ASP.NET webforms project .NET 4.0
Anyone?
UPDATE: it looks like the problem has to do with Red Gate Smart Assembly in combination with method parameters with default values.
UPDATE: Smart Assembly 6.9 fixes the error for me.
Original Snippet:
private void radButton1_Click(object sender, EventArgs e)
{
string perp = radTextBox1.Text;
int i = 0;
DataRow arp = ale.Rows[i];
while (i <= ale.Rows.Count)
{
if (ale.Rows[i].Field<>("FullName") = perp)
{
arp = ale.Rows[i];
ale.Rows.Remove(arp);
}
}
i = ale.Rows.Count;
radLabel1.Text = i.ToString();
}
Changed this:
if (ale.Rows[i].Field<>("FullName") = perp)
To This:
if (ale.Rows[i].Field<String>("FullName") == perp)
Got the same error (fresh installation of the VS2015 Enterprise, ASP.NET webforms project .NET 4.0).
After some investigation I've found that there are two DLLs in references which causes this. Both are .Net 2.0 assemblies and both of them obfuscated by Red Gate Smart Assembly 6.5. And the real reason is... obfuscation.
Luckily, these assemblies are mine, so I've tried to build them without using of Smart Assembly - error is gone.
Interesting is that no any errors or warnings shown by Visual Studio before trying to build a project.
Good luck!
EDIT: Updating Smart Assembly to version 6.9 fixed an issue.
As #Andrey reported this does appear to be an issue with obfuscated assemblies that is causing some difficulty for Roslyn. Today I was able to get a live repro of this error and the root cause appears to be the obfuscator is invalidating / corrupting how default parameter values are stored in metadata. When run through ildasm the default values will be displayed as:
.param [3] /* Invalid default value for 0800001F: */
The previous version of the compiler handled this scenario by treating the invalid value as null or default(T). We will be fixing Roslyn to have the same behavior.
I also had this exception being thrown in VB.NET (Visual Studio 2015 - Pro), and isolated a single line that was causing the error.
In the line of code below, if you define model as an integer, as in:
Dim model as Integer = 2
and then use:
Const N As Integer = model
you will throw the exception.
However, when I modified this to:
Dim N As Integer = model
the exception was not thrown. The Const syntax was legacy code from another program that I rapidly added model to, and constants can't be set to pre-defined integer types.
I just had "Failed to emit module". I mistakenly put empty brackets in a call to a generic extension method, only when inside a ternary operator, like this:
var a = b == 6 ? 8 : x.Foo<>();
(Outside of a ternary op, I just get regular error CS7003: Unexpected use of an unbound generic name.)
Extending on the answer from #jony-adamit:
I also had this compilation error being thrown in C# (Visual Studio 2015). It came down to the differences in compiler output from .NET 4.5 and Roslyn the new compiler in VS2015. You can take the below code and run it yourself or use dotnetfiddle (https://dotnetfiddle.net/fa2rMs) and switch between compilers.
In .NET 4.5 compiler (C# compiler version 12.0 or earlier) you get this message:
Compilation error (line 16, col 7): The type or namespace name
'Select' does not exist in the namespace 'Tools.Weapons' (are you
missing an assembly reference?)
In the Roslyn 1.0.0 and 1.1.0 compiler versions you get this message:
Compilation error (line 1, col 1): Failed to emit module
'MyAssembly'.
Code to reproduce the error (notice the namespace):
public class Program
{
public static void Main()
{
Tools.Delegater.Work();
}
}
namespace Tools
{
public static class Delegater
{
public static System.Action Work = () =>
{
var query = from x in Weapons
select x;
};
}
}
namespace Tools.Weapons
{
public class Cannon { }
}
As you can tell from the compiler messages, Roslyn's message leaves you guessing as to the what and the where about the compiler error. And depending on the size of the application, the lack of details could take days or weeks to discover the root cause. Whereas the message in the previous compiler pointed you to the exact spot to start reviewing your code.
Another big difference is the previous compiler will show you a syntax error in Visual Studio for this scenario, but unfortunately Roslyn (atm) does not. However, if you knew where to look and you hovered your mouse over the 'x' variable in the linq to sql statement, then you would see that Rosyln doesn't understand how to interpret it.
I was getting a similar error: "BC36970. Failed to emit module 'Something.dll'." I realize the error code is not the same but it is also a "failed to emit" type issue so I thought I'd share my answer.
Problem: My problem was that I had a string that was a constant but I was trying to append another string to it, like so (using VB code):
Dim MyString1 As String = "Test1"
Const MyString2 As String = "Test2" & MyString1
Solution: I simply had to convert the second string from Const to Dim and the error disappeared:
Dim MyString1 As String = "Test1"
Dim MyString2 As String = "Test2" & MyString1
I got this error when using a generic method but failing to include the type.
This code gave the error:
example.GetValue<>(foo);
Correction simply was to specify the type:
example.GetValue<string>(foo);
I'm not sure if the erroring example is valid syntax or not: I would have expected a syntax error if it is not rather than this error, and Intellisense accepted the syntax.
There's another bug that can cause this exact error:
Happened to me after changing a property name without refactoring, which caused some linq code to call the namespace itself!
To see the bug in action all you have to do is run this piece of code:
public static int Test()
{
var x = from c in ConsoleApplication5
select 3;
return x.First();
}
Where ConsoleApplication5 is the namespace!
This only happens in VS2015.
Update:
There's an issue for this now on GitHub if anyone's interested.
I got this error when I was working with data table and did not provide a data type where it was required:
bool temp = dtTab.AsEnumerable().Any(row => row.Field<>("active") == 1);
Giving it a proper data type got rid of the error. I also had to convert it to string to be able to compare it correctly-
bool temp = dtTab.AsEnumerable().Any(row => row.Field<Boolean>("active").toString() == "1");
I got this error too, seems like there is some major reasons that cause this error.
I posted this answer to wrap up mentioned solutions and help other guys so they can solve this error quickly
Using obfuscated DLLs by Smart Assembly mentioned on marked answer Solution: Update Smart Assembly to version 6.9
Namespace issue mentioned here, also has open Github issue Here
In my case the problem was this one, Field is strongly-typed, not specifying it, mentioned here
Using Const keyword, mentioned here
Please read all answers here on this page carefully, I'm sure you can solve the issue using one of mentioned solutions, good luck :)
This answer is similar to that posted by #wrtsvkrfm
'Dim NEWLINE As String = "<BR/>"
Dim NEWLINE As String = vbCrLf
Const HT_QTC_VENDOR As String = "<B>some stuff {0}</B>" & NEWLINE
This will cause the DLL to be not emitted,
Change the top 2 lines to
'Const NEWLINE As String = "<BR/>"
Const NEWLINE As String = vbCrLf
and the error goes away.
This should be a compilation error.
A CONST clearly cant depend on a variable, which may..., well..., vary. :-)
I get the same "Failed to emit module" error with this code but changing the "Const" to "Dim" resolves the problem:
Dim userStatus1 As String = convAttrSet("user_status")
Dim userStatus2 As String = convAttrSet("user_status")
Const PENDING As Boolean = (userStatus1 = userStatus2)
I got this error when using linq to entities and my linq query had a syntax error in the join clause
For me the issue occured after adding a postbuild step that changes the assemblyinfo.cs (NetRevisionTool.exe).
Take care to not change the AssemblyInfo.cs between building and Edit and continue.
I also had this exception being thrown in VB.NET (Visual Studio 2015 - Pro), and isolated a single line that was causing the error
Faulty....
Dim i As Short
Dim j As Short = BancoCombienElement(LaChaine)
Const a = (22 / 60) * j
Dim TiTxt As String = ""
Dim S_8_10_12_14 As String = ""
Work good!!!!!!!!
Dim i As Short
Dim j As Short = BancoCombienElement(LaChaine)
Dim a As Short = (22 / 60) * j
Dim TiTxt As String = ""
Dim S_8_10_12_14 As String = ""
Reason:
This error seems to be introduced when we have something weird in our code.
For me I made a mistake while getting the current user from identity.
Error Code:
int currentUserId = User.Identity.GetUserId<>();
Fixed Code:
Please specify the data type after GetUserID<---------->();In this case I have specified int.
Note
Please try to undo your code step by step so that you can find this issue. But otherwise it was very difficult for me track that error.
Thanks
This can happen for any error that the compiler did not catch. Probably
either the compiler doesn't know what IL to emit, or
it knows but in the process of actually doing it, something unexpected happens or
the IL that it emits is invalid and some safety net at a lower level than the compiler itself produces an error that the compiler is not built to handle.
In any case, this CS7038 occurs because the C# is valid and there is a good chance the compiler encountered an error in such an unexpected place that it is no longer able to trace it to any source code line that you wrote. Hence the seemingly random errors that people report here to cause this.
I've used C# for decades and I arrive here now because this is the first time ever I get an error like this.
In my case the following code generates the error. Use <LangVersion>Preview</LangVersion> in the project file and use the C# 8 compiler in Visual Studio 2019 preview.
void FailToEmitDll(ReadOnlySpan<char> span) {
ReadOnlySpan<char> temp;
temp = 1 switch
{
_ => span
};
}
Odd enough, if you change the declaration and the assignment to read like var temp instead, all works well.
Adding to the list of possible causes, in VS2019 with C# 8, I got the "Failed to emit module" message when doing something like:
_ = someBool ? SomeVoid() : default;
I filed a bug report about it here: https://github.com/dotnet/roslyn/issues/41741
I got this error when I run a complex expression in Mock object, I fixed this by store the expression result to local variable.
I can't get string interpolation to work. Last news from MS I found was
http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx
However all that is said there is not working. Anyone knows if string interpolation made it into VS 2015? Is there any documentation about it? Can one you give an example?
For instance, none of these formats work (edited):
int i = 42;
var s = "\{i}"; // correction after jon's answer: this works!
var s = $"{i}"; // compiler error
var s = "{{i}}"; // no interpolation
edit about VS 2015 CTP 6 (20.4.2015 )
The final version is
var s = $"{i}"
also supported by the current Resharper version
ReSharper 9.1.20150408.155143
Your first form did work in the VS2015 Preview:
int i = 42;
var s = "\{i}";
That compiled and ran for me. ReSharper complained, but that's a different matter.
For the final release of C#, it is:
var s = $"{i}";
String interpolation is making it to VS 2015. Its latest syntax (which wasn't ready for the preview, but made it into VS2015 CTP5) is this:
string s = $"{i}";
It also supports am IFormattable result using the FormattableString class:
IFormattable s = $"{i}";
The latest design documentation is here: String Interpolation for C# (v2)
You can check that online using the latest Roslyn version with http://tryroslyn.azurewebsites.net. Here's the specific example.
I am working on a calculation module using C#, and I bumped on this :
double v = 4 / 100;
I know this is a wrong initialization that returns v = 0.0 instead of v = 0.04
The c# rules says I must ensure at least one of the member is a double, like this :
double v = (double) 4 / 100;
double v = 4.0 / 100;
However, I have many many initializations of that kind that involves integer variables operations, and I feel lazy to browse my code line by line to detect such mistakes.
Instead, is it possible to get warned by the compiler about this ?
Alright, after some playing around and what not, I have a solution. I used this article to come to this solution.I use StyleCop, so you'll need to get and install that. Then, you can download my C# project MathematicsAnalyzer.
First off, I did not account for all type conversion mismatches. In fact, I only accommodate one part.
Basically, I check to see if the line contains "double" followed by a space. I do know that could lead to false warnings, because the end of a class could be double or any number of other things, but I'll leave that to you to figure out how to properly isolate the type.
If a match is found, I check to see that it matches this regex:
double[ ][A-Za-z0-9]*[ ]?=(([ ]?[0-9]*d[ ]?/[ ]?[0-9]*;)|[ ]?[0-9]*[ ]?/[ ]?[0-9]*d;)
If it does -not- match this regex, then I add a violation. What this regex will match on is any of the following:
double i=4d / 100;
double i = 4d / 100;
double i = 4 / 100d;
double i = 4/ 100d;
double i = 4 /100d;
double i = 4/100d;
double i=4d / 100;
double i=4 / 100d;
double i=4/100d;
Any of the above will not create a violation. As it is currently written, pretty much if a 'd' isn't used, it'll throw a violation. You'll need to add extra logic to account for the other possible ways of explicitly casting an operand. As I'm writing this, I've just realized that having a 'd' on both operands will most likely throw an exception. Whoops.
And lastly, I could not get StyleCop to display my violation properly. It kept giving me an error about the rule not existing, and even with a second pair of eyes on it, we could not find a solution, so I hacked it. The error shows the name of the rule you were trying to find, so I just put the name of the rule as something descriptive and included the line number in it.
To install the custom rule, build the MathematicalAnalyzer project. Close Visual Studio and copy the DLL into the StyleCop install directory. When you open Visual Studio, you should see the rule in the StyleCop settings. Step 5 and 6 of the article I used shows where to do that.
This only gets one violation at a time throughout the solution, so you'll have to fix the violation it shows, and run StyleCop again to find the next one. There may be a way around that, but I ran out of juice and stopped here.
Enjoy!
This article explains how to set up custom Code Analysis rules that, when you run Code Analysis, can show warnings and what not.
http://blog.tatham.oddie.com.au/2010/01/06/custom-code-analysis-rules-in-vs2010-and-how-to-make-them-run-in-fxcop-and-vs2008-too/