I'm writing a program that needs a priority queue, and it would be nice if I could use the one that comes with .Net 6: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.priorityqueue-2?view=net-6.0
So I've tried writing some sample code (in F#, but this particular code fragment could be written just the same in C# and I would expect the results to be the same):
open System.Collections.Generic
...
let pq = PriorityQueue()
pq.Enqueue("apple", 5)
pq.Enqueue("banana", 6)
checkEq (pq.Dequeue()) "apple"
checkEq (pq.Dequeue()) "banana"
Visual Studio is perfectly happy with that, autocomplete worked and everything.
I try to compile it, and fsc is not at all happy:
C:\olivine\src\test.fs(1345,14): error FS0039: The value or constructor 'PriorityQueue' is not defined.
Maybe that's because I don't have the right version of .Net installed.
reg query "HKLM\SOFTWARE\Microsoft\Net Framework Setup\NDP" /s
Needless to say, there seem to be dozens of versions installed, but the highest version numbers I am seeing, appear to be 4.x, which would explain why fsc is not happy.
But then what's up with Visual Studio? How did it accept and understand the reference to PriorityQueue, despite .Net 6 being apparently not installed on the machine?
Related
For learning purposes, I use csc.exe myprogram.cs shipped with Visual Studio Community (my version 17.3.4) to compile basic C# programs. It mostly works, except when using what appears to be latest language features, for example, array range shorthand array[0..] or element from end shothand array[^1].
For example, when trying to access [^1] element.
Arrays.cs(58,47): error CS0518: Predefined type 'System.Index' is not defined or imported
Arrays.cs(58,47): error CS0656: Missing compiler required member 'System.Index..ctor'
I tried passing -langversion:preview to csc but still not working. Also I was unable to find proper usings for it to work.
Can I somehow get those features to work with basic csc compilation? They worked when I created csproj file and used dotnet build, but doing so was multiple times slower than using just csc. Thanks!
Arrays.cs(58,47): error CS0518: Predefined type 'System.Index' is not defined or imported
Arrays.cs(58,47): error CS0656: Missing compiler required member 'System.Index..ctor'
The compiler uses several types to lower index / range expressions to IL. One of those types is System.Index. This error is the compiler noting that it cannot find that type which is necessary to lower that expression to IL.
This feature was added as a part of netcoreapp3.1 where csc defaults to compiling for .NET Framework applications. These types are not present in the standard set of references you get for .NET Framework hence this is why you get the error.
They worked when I created csproj file and used dotnet build ...
That worked because your project file contained something like the following:
<TargetFramework>net6.0</TargetFramework>
This targeted your application for .NET Core, the build command passed along the standard references for that and those included the System.Index type.
... but doing so was multiple times slower than using just csc.
Building a .NET application involves more than just compiling code. It is finding the correct set of references, building dependencies, compiling and deploying the resulting binaries. It's more work hence it's going to take longer to complete. Multiple times slower is not expected, particularly for repeated executions, but it will take more time that invoking csc, or any ofeth other tools used in build, directly.
I tried to include contracts into my project, something like:
public Segment Bounds() {
Contract.Ensures(segments.Length > 0, "Segments are not empty");
return new Segment(segments[0].a, segments.Last().b);
}
I get error (shown as message box) which said me that I should install CCRewrite. I got it
here, installed but "Contracts" tab in project settings was not appeared and I continue to get the same error. I'm using Visual Studio 2017 Community. Is it possible to use contracts with this version of Visual Studio?
From the CodeContract project wiki:
Visual Studio 2013 is the only version that is supported as a build environment for Code Contracts.
As pointed out in OP's comment, more background information can be found in the following issue:
I have the same issue, I bet on CC for some largish projects. As much as I love it and will miss it, I think everyone should seriously consider getting them out of their code base sooner rather than later. There is just not enough commitment there from either MS or the volunteers here (I don't blame them, it was always made clear that it's a non supported technology).
-- chrisaut
My conclusion (in accordance with #HansPassant's comment) is that the project is abandoned.
How do I configure VS 2015 to enable Roslyn's C# scripting capabilities?
I've tried installing various Nuget packages, including both the 1.0 and 1.1.0-beta1 versions of Microsoft.CodeAnalysis.CSharp, Microsoft.CodeAnalysis.Scripting.CSharp, etc., but I can't get any of the examples I've found online to work. I am getting "type not found" errors, i.e.,
var scriptEngine = new ScriptEngine();
... fails because the type "ScriptEngine" is not found.
Can someone provide as recipe that includes which nuget packages to install, which using statements are required, etc., to implement Roslyn scripting?
UPDATE #1:
I have made some progress, but still having issues. I get a bunch of compiler warnings and then a TypeInitilizationException which is apparently due to a component version mismatch.
I'm now using the following example code (taken from a test), and there's no missing types:
using System;
using Microsoft.CodeAnalysis.Scripting.CSharp;
namespace RoslynScriptingTest {
class Program {
static void Main(string[] args) {
var script = CSharpScript.Create("1 + 2");
var fn = script.CreateDelegate();
var value = fn();
Console.WriteLine("value={0}", value.ToString());
}
}
}
I've loaded all of the nightly packages that are available at https://www.myget.org/F/roslyn-nightly/.
I get a series of build warnings that refer to Microsoft.CodeAnalysis, v1.1.0.0.
Running the exe despite the warnings yields the TypeInitilizationException mentioned above. Based on the stacktrace, the TypeInitializationError is caused by a version mismatch for System.Reflection.Metadata.dll.
I am not sure where to go from here. I don't understand how the scripting-related packages/components fit together. I saw some posts from earlier this year that describe building Roslyn completely. I have not done that. Is that necessary?
This is reminding me of DLL hell from the old days.
The scripting APIs are still in progress, and were removed from the release packages.
Try the nightlies instead.
With visual studio 2015 update1 the REPL is back and scripting api's are enabled.
Here is what Microsoft says about it:
In this release, the C# Interactive Window is back in Visual Studio, as well as the command-line C# REPL window. (The Interactive Window is the REPL window inside Visual Studio.)
We've also released scripting APIs that enable you to build and run C# as a script. The scripting APIs are available on GitHub.
Additionally, we've released csi.exe, which is a tool that you can use to run a C# script file (.csx) from the Developer Command Prompt. For example, simply type csi myScript.csx to run your script file. Or, you can enter the command-line REPL mode to interactively evaluate snippets of C# code. To get to this mode, run the command csi without any arguments from the Developer Command Prompt.
Reference:
https://www.visualstudio.com/news/vs2015-update1-vs#Csharp
I'm writing a C# Gtk# application under Mono/MonoDevelop in Linux Mint 17. I have a ComboBox on a form with 3 items in it. I was able to place it and such fine however, I get an error in the designer code stating that the ComboBox doesn't contain a definition for 'NewText'. The Mono documentation at docs.go-mono.com says that this NewText method is how you add items, however, apparently mine doesn't have it.
My MonoDevelop version is 4.0.12
The projects target Gtk# version is 3.0
I have the following references and their versions referenced in the project
And finally, the designer code
//top of file declaration
private global::Gtk.ComboBox framestyle_Val;
//further down, this defines all the items
this.framestyle_Val = global::Gtk.ComboBox.NewText ();
this.framestyle_Val.AppendText (global::Mono.Unix.Catalog.GetString ("Single Sprite"));
this.framestyle_Val.AppendText (global::Mono.Unix.Catalog.GetString ("Left/Right Sprites"));
this.framestyle_Val.AppendText (global::Mono.Unix.Catalog.GetString ("Left/Right/Upside-Down Sprites"));
It's worth noting that apparently there's no AppendText definition/method either but I assume that'll be straightened out once we get this one straightened out.
Any ideas? I'm confused myself. Thanks in advanced,
Mike
I had the same problem. MonoDevelop seems to be unable to generate proper code to use with Gtk#3.
You may want to install Gtk#2, this will not override the GTK#3 installation, and target your project with this version to be able to use MonoDevelop's visual design abilities.
And maybe you will need to create the clean GTK#2 project once again.
If you have found a solution to use GTK3, please give me an idea.
How can a C# program detect it is being compiled under a version of C# that does not contain support for the language features used in that program?
The C# compiler will reject the program, and produce some error message, on encountering the features of the language it does not support. This does not address the issue, which is to state that the program is being compiled with too old a version of the C# compiler, or a C# compiler that does not support the required version of C#
Ideally, it would be as simple as
#if CS_VERSION < 3
#error CSharp 3 or later is required
#end
I don't believe you can do that with a C# file, but if you're using MSBuild then the project/solution tools version number can stop it from being built with an older version of MSBuild.
Can you give the exact context of this? One "human" solution rather than a technical one might be to try compiling the code with all the "old" versions, and create a document with: "If you get an error like this it probably means you're using the wrong version..."
Another option you might want to consider to make that even simpler is to have a single "required features" file. This would be unused by your main app, but ask users to compile that first. If it works, the rest of your build should work. If it doesn't, it's due to using the wrong version. That's likely to produce a smaller range of errors from different versions (in particular it doesn't have the problem that the compiler could list errors from different files in a random order).
According to this list of preprocessor directives, it doesn't seem possible. We usually can tell by using generics (detects 2.0), using auto properties (3.0) or dynamic (4.0)
Easy: The compiler will fail and give you an error if it can't compile the code.
There is no predefined symbol for this; you could create your own symbols and define them in your build script. Note that you can fix the language version at the project level:
Project properties -> Build -> Advanced -> Language Version:
ISO-1 is C# 1.2
ISO-2 is C# 2.0
(maps to the csc /langversion parameter)
But note that this doesn't cover everything - there are a few things that this will let through - particularly in the area of generic type inference. This is only an issue if you need to support old compilers; in which case... test with an old compiler.
For example:
int[] arr1 = { 1, 2, 3, 4, 5 };
string[] arr2 = Array.ConvertAll(arr1, delegate (int i) {return i.ToString();});
This works in .NET 3.5/ISO-2 (pseudo C# 2.0), but doesn't work in .NET 2.0 (proper C# 2.0).
Perhaps you can read the supportedRuntime element:
Then you can define the directive you want based on the supportedRuntime, using CSharpProjectConfigurationProperties3.DefineConstants.
You would create a macro in VS to use this.