I wonder if there is something like a standalone Version of Visual Studios "Immediate Window"? Sometimes I just want to test some simple stuff, like "DateTime.Parse("blah")" to see if that works. But everytime i have to create a new console application, put in my code and test it.
The Immediate Window sadly only works when I am debugging something. Could PowerShell do that? Just open a CLI similar to what cmd.exe does, allowing me to execute some C# code?
Linqpad - I use it like this all the time. http://www.linqpad.net/
Don't be misled by the name - that just describes the original motivation for it, not its functionality.
Just recently he released a version with proper statement completion - that's a chargeable add-on (the core tool is free), but a minute amount of money and well worth it, I think.
The Mono project includes an interactive C# shell, this may be just what you're looking for.
http://www.mono-project.com/CsharpRepl
C# Interactive window and csi.exe REPL were added to Visual Studio 2015 Update 1:
Introducing Interactive
The Interactive Window is back! The C# Interactive Window returns in Visual Studio 2015 Update 1 along with a couple other interactive treats:
C# Interactive. The C# Interactive window is essentially a read-eval-print-loop (REPL) that allows you to play and explore with .NET technologies while taking advantage of editor features like IntelliSense, syntax-coloring, etc. Learn more about how to use C# Interactive on Channel 9 or by reading our beginner’s walkthrough.
csi. If you don’t want to open Visual Studio to play around with C# or run a script file, you can access the interactive engine from the Developer Command Prompt. Type csi /path/myScript.csx to execute a script file or type simply csi to drop inside the command-line REPL.
Scripting APIs. The Scripting APIs give you the ability to execute snippets of C# code in a host-created execution environment. You can learn more about how to create your own C# script engine by checking out our code samples.
See What’s New in Visual Studio Update 1 for .NET Managed Languages.
Basically, now you have:
IDE REPL — C# Interactive window in VS
Script interpreter — csi foo.csx from Dev Cmd Prompt
Command line REPL — csi from Dev Cmd Prompt
Scripting API
Try scriptcs, it's not integrated into the VS IDE but it does let you type and run C# in a script window without the need for a project compiler etc...
Well, this isn't a direct answer to your question, but you could look at this tool:
Snippet Compiler
Also, if you want to see the IL produced, or similar, there is a tool that plugs into Reflector, called Snippy, based on the Snippy tool that Jon mentions in his own answer further down.
All of these are very nice to use.
As you suggest, PowerShell can do what you want. For example, to test your DateTime.Parse, the following one liner will do the trick:
PS C:\Documents and Settings\Dan> [System.DateTime]::Parse("Blah")
Exception calling "Parse" with "1" argument(s): "The string was not recognized as a valid DateTime. There is a unknown
word starting at index 0."
At line:1 char:25
+ [System.DateTime]::Parse( <<<< "Blah")
PS C:\Documents and Settings\Dan> [System.DateTime]::Parse("1/2/3")
01 February 2003 00:00:00
Note that the above uses the current release of PowerShell (v1.0). The next version of PowerShell will allows you to intermingle C# with PowerShell scripts more directly. To whet your appetite, watch this 7 minute screencast "C# to PowerShell" by Doug Finke. Very impressive!
If you're using Mono, there's this:
CsharpRepl
Don Box hacked something very simple up a few years ago too.
Along the lines of lassevk's answer, I've got "Snippy". This was developed for C# in Depth, and the UI is pretty rubbish, but it works - and lets you write extra members (methods, nested classes etc) as well, e.g.
public static void Foo()
{
Console.WriteLine("Hello");
}
...
Foo();
(The ... is used to tell Snippy "everything under here belongs in Main".)
We've just released CShell a full featured C# REPL IDE. It supports code completion, script files, adding references and is really extensible. Also we plan to add NuGet support soon, which will make it super quick to write some code and see how it works.
http://cshell.net/
We love LINQPad but it doesn't have a REPL, the code is executed once and you cannot do anything further with the results unless you modify the script and run the whole script again. This is okey, but sometimes if you want even more a scripty feeling then to evalute your code in a REPL is really nice and convenient.
I also find that SharpDevelop is so quick and lightweight that it is the easiest way to whip off a quick test project.
You may find the Object Test Bench useful. It's not very well known, but lets you create instances of classes, execute static methods and so on. It can be useful for discovering how to use unfamiliar APIs or for quick debugging of your own classes and methods, saving the creation of a test harness for simple checks.
You can find the MSDN documentation here:
http://msdn.microsoft.com/en-us/library/c3775d98%28VS.80%29.aspx
If you could wait a while.. it looks like there could be a C# equivalent of Ruby's irb in time for C# 4.0
Anders H. demonstrated an interactive console session where you could type in arbitrary C# code and see results in his 'Future of C#' piece at PDC 2008. You could even pop a WPF Window from it and then play with it via the console interface. Pretty cool.
Use LINQPad.
Name notwithstanding, it can execute any C# or VB code, from simple expressions to entire classes.
Plus, it can visualize entire object graphs in the results.
You can even add references to your own assemblies.
Try the C# REPL Script Environment that is part of the O2 Platform. It is exactly what you are asking for.
It will give you a perfect environment to try out all C# features and APIs (conceptually the O2 REPL environment is similar to LinqPAD)
You should also take a look at Roslyn from Microsoft. On Multiple Roslyn based tools (all running Stand-Alone outside VisualStudio) , the first one is a simple Roslyn REPL
If you happen to know and like Python, then IronPython may be a good alternative. It comes with a (Python) REPL console (ipy.exe) where you can import and use all of the .Net types.
I find it useful for testing out little things, exactly like DateTime.Parse("Blah").
Note that it can't actually execute C# code, but if all you want is access to .Net, then it's perfect. Also, if you install the IronPython Tools for VS, you can start a REPL session right in VS using a single keyboard shortcut (Alt+I) and leave it running in a docked window for when you need it.
Update: http://avillenas.com/post/interactive-c
Well, you can try this.
Download and install dotnet.github.io
Open cmd
Type "dotnet repl"
Type whatever you need.
I did what you wanted to do. Click here to see the gif
The Roslyn project sources contain a REPL called CSI (http://sourceroslyn.io/#csi/Csi.cs). The Csi class is currently internal but with the the “csi” project (Roslyn.sln: Interactive/Hosts/csi) an executable console application is available that supports e. g. the command #r to load an assembly and #load to load and execute script files (start the csi and use #help).
Related
I'm beginner to C# and have to develop some C# code under linux.
Until now, I could work it all out in a single file, just by using the two commands mcs and mono, without any GUI. And if possible, I'd like to avoid the GUI.
But now I want to seperate code in different files or even folders, but I could not find any explanation online.
Let's say I have some code like this
public class SpecialVector
{
// some code
}
in some special.cs file. How can I use SpecialVector in some other file, let's say normal.cs? (Btw how do you call code grouped in a file in C#?)
Excuse me if this question has already been asked, but I don't know how to twist it so that the search engine understands my problem...
As has been mentioned in the comments, if you aren't limited to Mono for a particular reason, you are highly encouraged to switch to .NET Core (now called .NET as of version 5). It has native support for both Windows, Mac and Linux, and comes with command line tools that allow you to create and build entire multi-file projects in a single command, without having to specify each individual file manually.
That being said, if you continue using Mono, you can specify multiple files to the compiler in the following manner:
mcs File1.cs File2.cs
(and so on)
Have a look at the man page for mcs for more info.
I'm wondering if anyone can help.
I have an c# application to make modifications to multiple in house msi files.
We want to run ICE validation and get the results back in my application. I know i can run MsiVal2.exe as a process and output the results back to the app but i would ideally like to run it all via code.
There is an evalcom2.dll which appears to be written in c++ but there seems to be very little information around using this.
Im surprised that nobody seems to have done this or is there an easier way like using one of the wix libraries or some thing?
Any Help would be much appreciated.
WiX Toolkit: I don't really use it manually that much (I validate using Orca), but the WiX toolset itself contains the binary smoke.exe. I believe it is run for every build in Visual Studio? Not sure. The full source available on github.com.
Looks possible to adapt to your own project. You could also just pass it to the compiled smoke.exe I guess. In the code, you can see:
Line 180: validator.Validate(Path.GetFullPath(inputFile));
Validator.cs is in the WiX project (https://github.com/wixtoolset/wix3/tree/develop/src/tools/wix)
Think about the .NET Framework instructions Debugger.IsAttached() or Debugger.Break(), they make easy to interact with an external exe (the debugger) from inside the source code.
Is it possible to do something similar with Resharper? For example to do a full text search on the current solution:
You can write a ReSharper plugin using ReSharper SDK.
Is there an interactive shell for programming quick code snippets in C#/.NET? Sometimes I'm interested in trying out a quick block of .NET code without creating a whole console application (that I then have to delete when I'm done).
I understand that the Immediate Window sort of provides this functionality, but it seems like you have to have an application open and be debugging it for the Immediate Window to be of any use.
I was tinkering with Ruby a while back, and the Interactive Ruby Shell (irb) was an invaluable little tool for trying out little snippets of Ruby code. Lightweight and instantaneous. This kind of thing seems like a no-brainer to me. Does anything like it exist for C#/.NET?
LINQPad is very neat for that:
LINQPad is more than a LINQ tool: it's an ergonomic C#/VB/F#
scratchpad that instantly executes any expression, statement block or
program with rich output formatting – the ultimate in dynamic
development. Put an end to those hundreds of Visual Studio Console
projects cluttering your source folder!
There isn't a full interactive shell for C#. However, for many things, you can use LINQPad to quickly try out a snippet of C# code. While it's intended for LINQ testing, it works for other C# code as well.
For details, see LINQPad as a Code Snippet IDE.
The Mono project has an interactive shell.
Also, you can use the Snippet Compiler.
There was one published by the Mono Project a while back.
Yes, there's one for mono mono-project.com/CsharpRepl
I know this is an old post, but I am new to C#. I actually found a dotnet tool that is pretty much like Java's jshell. To install simply go to powershell and enter:
dotnet tool install dotnet-repl --global
dotnet-repl.exe
Screen Shot of dotnet-repl
We've just released a small REPL tool for C# with code completion and everything called CShell.
http://cshell.net
You can also write scrips and then send them to the REPL or just write your snippets right in the REPL.
It's still in beta, but it works pretty well!
.Net Fiddle https://dotnetfiddle.net/ is an option. Not exactly an interactive environment where you can try out any expression. In .Net Fiddle, you can change the project type, compiler version, etc.
LINQPad is very good, but if you are into a quick (no installation) C# snippet testing without external dependencies (files, databases etc.) you can also use Rextester.
However, notice that its compiler version is 4.0.30319.17929, so fancy things from C# 6.0 like interpolated strings, null conditional, property initializers will not be available.
It is called snippetcompiler, will search for the url...
MonoDevelop 1.0 doesn't appear to have a code-formatter like Eclipse does for Java. Is there a preferred shell script (or MonoDevelop add-in?) that you've found to work well?
At the moment, source code formatting in MonoDevelop is marked as a future enhancement:
https://bugzilla.novell.com/show_bug.cgi?id=321946
In the meantime, you may want to check out Artistic Style for C# formatting:
http://astyle.sourceforge.net/
I'm planning to see how this might be wired up as an external tool within MonoDevelop. If I get to that, I will edit my answer with the information. UPDATE: I don't have enough reputation to leave a comment, so I'll make one here: Nice job, Dustin, and patch for MonoDevelop too :-) I wonder how recent the version is that is included with Ubuntu... Either way, I'm glad you found something that works for you.
Thanks, Brandon. I submitted a patch to MonoDevelop. The issue with MonoDevelop add-ins is that there is a mono compiler bug that doesn't handle anonymous delegates correctly. (bug report: https://bugzilla.novell.com/show_bug.cgi?id=394347)
The patch/workaround is to just cast the anonymous delegate to the proper delegate type. (bug report & patch: https://bugzilla.novell.com/show_bug.cgi?id=369538)
I'm running with the patched version now and am able to execute AStyle on the currently edited document by simply creating a new External Tool setting with the following settings:
TITLE: A_Style (put in an underscore _ to enable hotkeys)
COMMAND: astyle
ARGUMENTS: ${ItemPath}
Then, just execute it using Tools->AStyle (or ALT-T, S)
9/25/08 Edit -- I just put up a blog posting on how to patch MonoDevelop 1.0 and get it working with AStyle: http://dustinbreese.blogspot.com/2008/09/auto-formatting-code-in-monodevelop-10.html
It's a nice programming exercise to write your own formatter . I wrote one for C++ , and it was a nice challenge . You could learn a lot by writing it :)