C# Code Formatter for Linux and/or MonoDevelop - c#

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 :)

Related

Stetic GUI Designer Move to Glade for Gtk#3

I'm currently working at an audiobackend binding for my application. Since I want to use gstreamer, I found out, that I need to use GTK#3, because this is needed by gstreamer-sharp. Since I'm currently working with monodevelop, which uses stetic for GUI design, I wanted to ask, what is the best way, moving towards GTK#3. I see, that it uses .ui files for GUI definition, which can be generated by glade (I also tried a little bit around with glade). But my application has 4 windows with much code, so how can I easily transport this to GTK#3? Has anybody done this already?
Greeting
Sven
GtkBuilder works for both GTK2 and GTK3, Smuxi will eventually go that route and backport the GtkBuilder converted .ui files from Stetic back to GTK2.
The tool to convert the existing stetic UIs can be found here:
https://github.com/xDarkice/stetic2ui
Smuxi was a GTK2 app made with stetic too. When we did a Gnome .NET hackfest recently in Austria they found the problems you are talking about, but eventually solved them by using glade files and some clever approach to still be compatible with GTK2 and GTK3 at the same time, so you might want to look at their commits from September to November of 2013.

Cannot resolve symbol 'Void'

At the head of my class (I am taking over a project from someone else), I have the following:
using Java.Util;
using Object = Java.Lang.Object;
using Void = Java.Lang.Void;
Void, is showing in red, with a "Cannot resolve symbol 'Void'" and cannot see what I may be missing.
I have checked the PC where this came from (and the application does build on that one) and have installed the same (yes, I saw, newer, but specifically installed the "same" items) Java items:
Java 7 Update 45
Java 6 Update 31
Java SE Development Kit 6 Update 31
This hasn't made any change.
I have checked the 'References' branch of the project and everything is showing the same (in fact, I cannot see a specific reference to Java anywhere)
Any help would be appreciated to sort this.
Please understand that I am not looking to change the code; just to get the code (as is) to compile without error.
[Update]
When I type out Java.Lang. the intellisense does not show Void in the dropdown list
[Further Update]
This project is tied to the development for a tablet; for which I hadn't installed some of the development software - installing these has resolved the issue and I can only assume that the Java namespace is buried within the software. Whilst there's no specific answer in the responses, most of the comments have certainly helped to steer me in the right direction.
There are several possible ways a C# project consuming Java bits,
Microsoft J#, which has been dead for a long time.
IKVM.NET, which originates from Mono.
dot42, a C# to Android compiler, which compiles C# to Android.
Xamarin.Android (aka Mono.Android).
Based on the fragment you pasted I think you might be opening a Xamarin.Android project in Visual Studio, as the using clauses match,
http://androidapi.xamarin.com/?link=N%3aJava.Util
http://androidapi.xamarin.com/?link=T%3aJava.Lang.Void
http://androidapi.xamarin.com/?link=T%3aJava.Lang.Object
Thus, you need to have Xamarin.Android completely installed on this machine. What you have (JRE and JDK) is obviously not enough.
dot42 does not have Java.Lang.Void and Java.Lang.Object, http://docs.dot42.com/Reference/NS.Java.Lang
I think these links would be helpful for you.
What is the difference between java.lang.Void and void?
http://docs.oracle.com/javase/7/docs/api/java/lang/class-use/Void.html
FAIL:
using Java.Lang.Void;
import Java.Lang.Void;
SUCCESS:
import java.lang.Void;
You have to use import instead of using.
import Java.Util.*;
Object blah = new Java.Lang.Object;
If you want to create an object that is null, do this:
Object obj = new Object();
If you want to add data set it equal to data such as a number.
obj = 7;

Is there an interactive shell for programming quick C# code snippets?

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...

Getting csharp-mode Emacs syntax highlighting working

Googling "csharp mode emacs" yields the page
http://www.emacswiki.org/emacs/CSharpMode
which includes a few links to various downloadable emacs lisp files. The 2005 link (DylanMoonfire) is broken, so I downloaded:
http://lists.ximian.com/pipermail/mono-list/2002-May/006182.html
as ~/.emacslib/csharp-mode.el
and added:
(autoload 'csharp-mode "csharp-mode"
"Major mode for editing C# code." t)
(setq auto-mode-alist (cons '( "\\.cs\\'" . csharp-mode ) auto-mode-alist ))
to my .emacs file (anywhere, beginning, middle or end). I attempt to edit a new text file called "t.cs" and I get the error:
File mode specification error: (error "Buffer t.cs is not a CC Mode buffer (c-set-style)")
and no syntax highlighting. I'm not well versed in emacs-lisp but I know enough to install support for loads of language modes and csharp-mode is just not playing nice compared to every other language mode I've installed.
I was getting excited to play with Mono on my Mac and ran into this ridiculous barrier! Anyone out there know how to get decent support for C# syntax highlighting in emacs?
Note: I'm using a MacBook Pro running Emacs 22.1.1 on OS X Leopard.
I found a more recent version of csharp-mode 0.7.0, go to the end of the page and download as plain text. Haven't tried loading the mode however.
Just uploaded the code to emacswiki as well: charp-mode.el
What you have seems to be very outdated. It's too bad that the more up-to-date version was not hosted somewhere safer, like the Emacs core, EmacsWiki, or Github.
But anyway, you might as well try asking on #emacs (Freenode) and see if someone can send you the more up-to-date file.
I made a slight modification to make Tab indent 4 spaces and deal with braces correctly, so now hitting tab on any line "does the right thing" with respect to conventions in Visual Studio.
http://www.updike.org/files/csharp-mode.el
The EmacsWiki page for CSharpMode says that the most current version of this package can always be found on github (josteink/csharp-mode).
Maybe you would like to use OmniSharp-Emacs that provides IDE-like features for editing files in C# solutions in Emacs.
See also the article Emacs as a C# development environment.

C# Console/CLI Interpreter?

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).

Categories

Resources