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.
Related
I would like to create an R-Package as R interface for a C# program.
At the moment it works quite well by just using the shell or system function on windows.
shell("C:/path/to/executable.exe")
This is clearly not satisfying as an .exe file is not working on Linux or MAC.
As I have the possibility to include the C# source code into my package and theoretically compile it while installing it on any OS, I was wondering if and how I can achieve that.
The rClr package seems to be an option but I am having trouble to understand how to actually use it for my purpose.
So my first question is:
Is it possible to do what I am trying to do or is it simply not possible and should I discard my wish to make the package usable on all OS?
If so it would save me a lot of time and I can do something else.
My second question would than be: If it is possible, how can I do it and where do I start?
In my application, I have provision to run Python scripts through IronPython. In one of the requirement, the data from my C# application should be displayed in 3D using Visualization Toolkit. When I try to use VTK in a script and execute it, I get "ImportError: No module named vtk" error. I have searched for file named vtkCommonPython.pyd but cannot find it on my computer. Should Python be installed even after installing IronPython? What is the correct way to use VTK from IronPython?
In most cases, IronPython does not work with native/CPython modules like what vtkCommonPython.pyd seems to be. You will probably want to look at .NET bindings for VTK in order to use it from your IronPython scripts.
ActiViz.NET might be what you should be using. Wiki/Documentation, Download
Other modules containing only pure python should typically work fine. It's 'just' the native extensions that do not work as there is no easy (and built-in) way to make native code callable from .NET/IronPython using the same conventions as in CPython.
For additional info see the FAQ ('Compatibility') and the issue tracker.
You might also want to keep an eye on the recent effort to revive and port ironclad to IronPython 2.7. This project aims to provide the marshalling layer required to invoke CPython modules.
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...
Alright, so I'm working on programming my own installer in C#, and what I'd like to do is something along the lines of put the files in the .exe, so I can do
File.Copy(file, filedir);
Or, if this isn't possible, is there another way of doing what I am attempting to do?
I wouldn't code my own installer, but if you truely want to embed files into your assembly you could use strongly typed resources. In the properties dialog of your project open up the "Resources" tab and then add your file. You'll then be able to get the file using:
ProjectNamespace.Properties.Resources.MyFile
Then you'll be able to write the embedded resource to disk using:
System.IO.File.WriteAllBytes(#"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);
Honestly, I would suggest you NOT create your own installer. There are many many issues with creating installers. Even the big installer makers don't make their own actual installers anymore, they just create custom MSI packages.
Use Mirosoft Installer (MSI). It's the right thing to do. Make your own custom front-end for it, but don't recreate the already very complex wheel that exists.
UPDATE: If you're just doing this for learning, then I would shy away from thinking of it as "an installer". You might be tempted to take your "research" and use it someday, and frankly, that's how we end up with so many problems when new versions of Windows come out. People create their own wheels with assumptions that aren't valid.
What you're really trying to do is called "packaging", and you really have to become intimately familiar with the Executable PE format, because you're talking about changing the structure of the PE image on disk.
You can simulate it, to a point, with putting files in resources, but that's not really what installers, or self-extractors do.
Here's a link to Self-Extractor tutorial, but it's not in C#.
I don't know enough about the .NET PE requirements to know if you can do this in with a managed code executable or not.
UPDATE2: This is probably more of what you're looking for, it embeds files in the resource, but as I said, it's not really the way professional installers or self-extractors do it. I think there are various limitations on what you can embed as resources. But here's the like to a Self-Extractor Demo written in C#.
I'm guessing here, but if you are trying to store resources in your application before compilation, you can in the Project Explorer, right click a file you would like to add, chose properties and change the type to Embedded Resource.
You can then access the embedded resources later by using the instructions from this KB:
http://support.microsoft.com/kb/319292
in case you simply want to store multiple files in a single file storage (and extract files from there, interact etc.) you might also want to check out NFileStorage, a .net file storage. written in 100% .NET C# with all sources included. It also comes with a command line interpreter that allows interaction from the command line.
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).