CSharpMode

This is a major-mode for editing C# files.

The most current version of this package can always be found on github:

It provides an editing mode for C# code and includes code indentation, fontification, codedoc generation, and integration with imenu.el.

Note:

nXhtml includes a slightly modified version of latest csharp-mode.el.

Usage Notes and Hints

Imenu

Imenu support is new for v0.8.3. It indexes the entire .cs buffer and produces a menu of the classes, methods, properties, etc. For large buffers this can cause a delay in opening a file, of several seconds. If you don’t want this, see ‘csharp-want-imenu’ .

Flymake

To use flymake, just open a C# file. If you have flymake loaded (you can do (require ‘flymake) in your csharp-mode-hook fn) then it will automatically start working. The flymake stuff in csharp-mode assumes csc.exe is on your path; if it is not, you can explicitly specify the flymake command to use in code comments, like this:

http://i.imgur.com/28V7e.png

You can run FxCop for your flymake. This is really handy. To do so, you need at least v0.8.3, and you need a comment like this near the top of your file:

// flymake: c:\fxcop\FxCopcmd.exe /c /rid:-Microsoft.Naming#CA1704  /f:Base64stream.dll

All of the issues found by flymake will be highlighted in the buffer.

If you use Flymake - with any language - you may want to check out some complementary stuff.

http://i.imgur.com/SgoLa.png

Neither FlymakeCursor nor RFringe is part of csharp-mode or flymake; they are complementary, add-on things.

Compile

csharp-mode sets the ‘compilation-error-regexp-alist-alist’, so that error messages emitted by csc.exe will be properly handled by next-error and prev-error. The default build command is ‘nmake’ but you can specify it explicitly in each buffer with in-code comments:

// compile: c:\.NET3.5\csc.exe /t:module /r:MyAssembly.dll file.cs

Example:

http://i.imgur.com/28V7e.png

You can replace that compile command with whatever you like. For example:

// compile: msbuild.exe /p:Configuration:Release

YASnippet

yasnippet is a nice snippet expansion package, available elsewhere. csharp-mode defines some snippets automatically for you, and makes them available if you have yasnippet. This means you can type for<TAB> and get a template of a for loop. There are snips for switch statements, properties, while loops, and so on. Pretty handy. You can also define your own snips.

CSharpRepl

This starts the repl on C-c C-z:

(defun my-csharp-repl ()
    "Switch to the CSharpRepl buffer, creating it if necessary."
    (interactive)
    (if-let ((buf (get-buffer "*CSharpRepl*")))
        (pop-to-buffer buf)
      (when-let ((b (make-comint "CSharpRepl" "csharp")))
        (switch-to-buffer-other-window b))))
(define-key csharp-mode-map (kbd "C-c C-z") 'my-csharp-repl)

Other tidbits

There’s codedoc support starting with version v0.7.2 (but you should really upgrade to v0.8.?). For the code doc: position point above a method/class, and type three slashes; you will get a codedoc template inserted into the buffer, similar to the effect you may have seen in VisualStudio. Fill-paragraph works as expected in the documentation string; there should be no need to setq paragraph-separate in your csharp-mode hook function. – DinoChiesa

Example:

http://imgur.com/vFvf3.gif

It tries to be smart about which template to insert. If you type 3 slashes after a closing </para>, then you’ll get another set of <para></para> .

http://i44.tinypic.com/zl99bc.jpg

csharp-mode Works with Linum.el :

http://i42.tinypic.com/29pdmjd.jpg


Complementary Stuff / Minor Modes

yasnippet.eldynamically expand snippets, like dabbrev on steroids. How to use yasnippet with C#.
ImenuModeproduce an index of the contents of a C# buffer.
HideShowhide and show regions of code.How to use hideshow.el with C#
FlymakeCsharpnow included with csharp-mode. check syntax more-or-less continually, and use overlays to highlight syntax problems in the code.
FlymakeCursordisplay flymake messages in the minibuffer, automatically, after a delay.
aspx-mode.elmulti-mode for editing ASPX files within emacs. Handles HTML, JS, CSS, and C#.
web-mode.elmulti-mode for editing ASPX/JSP/PHP that also embed JS and CSS.
MSTFSEmacs integration with Team foundation server, via the tf.exe tool
Linum.elline numbers (See local elisp: Lisp:linum-ex.el)
Auto-Revertrefresh .cs buffers, after you’ve edited them externally, for example, in Visual Studio.
code completionCode completion elisp logic, that is integrated with csharp-mode. It runs powershell as an inferior shell within emacs, to dynamically compile code snippets, and then introspect the result to provide realtime (subsecond) code completion in csharp buffers. To get it to work, you need the C# code, and be sure to read the readme. Works only on Windows. Requires Powershell (free). Does not require Visual Studio, or SharpDevelop.
vj-completeIs C# Code Completion in emacs really possible? This is an alternative code completion effort that pre-dates the above.
ide-bridgeC# Code Completion/refactoring/navigation/compilation. Integrates Emacs with SharpDevelop.
Omnisharp-EmacsProvides smart completion, refactoring, finding implementations/usages, eldoc integration and more.

Comments / Remarks / Gripes

With Omnisharp, it's amazing.

I have been using csharp-mode everyday in combination with Omnisharp-Emacs and it has been a joy. I am able to manage large solutions, and do my building from emacs. The code completion, and IDE like features from omnisharp provide little reason to use Visual Studio any longer. The base of csharp-mode is old and not updated, but it handles indenting and fontification and that’s really all I need from it.

Folding tip

I use the following hide-show setup for folding brace-blocks (just like c/c++ mode)

(unless (assoc 'csharp-mode hs-special-modes-alist)
          (push '(csharp-mode
                  "{" "}"
                  "/[*/]"
                  nil
                  hs-c-like-adjust-block-beginning)
                hs-special-modes-alist))

I also wrote a lineup function for multiline strings:

string foo = @"My very long string " +
              "that spans multiple lines.";

Here it is:

(defun csharp-lineup-string-cont (langelem)
  "Like `c-lineup-string-cont' but works with csharp string continuations."
  (save-excursion
    (back-to-indentation)
    (and (looking-at "@?\\s\"")
         (let ((quote (if (equal (char-after) ?@)
                          (char-after (1+ (point)))
                        (char-after)))
               pos)
           (while (and (progn (c-backward-syntactic-ws)
                              (when (eq (char-before) ?+)
                                (backward-char)
                                (c-backward-syntactic-ws))
                              (eq (char-before) quote))
                       (c-safe (c-backward-sexp) t)
                       ;; uncomment this to lineup under the @
                       ;;(progn (if (eq (char-before) ?@) (backward-char)) t)
                       (/= (setq pos (point)) (c-point 'boi))))
           (when pos
             (goto-char pos)
             (vector (current-column)))))))

Use for the statement-cont option in c-offsets-alist. I use it like so:

  (statement-cont . (my-lineup-csharp-string-cont +))

DaveS

Some History: previous attempts at C# mode

http://mfgames.com/csharp-mode
Work by DylanMoonfire on a derived cc-mode. See these threads on sourceforge [1], [2] for more info. Bug Tracking (Broken link as of December 2009)
The above link was changed to point people to the Google Code page since I’m not working on it anymore. I left the link to the historical releases on the site. There were no reported issues when the bug tracker was shut down at the end of 2009. – DylanMoonfire (November 2010)
http://www.cybercom.net/~zbrad/DotNet/Emacs/
This is the original. It involves changes to a set of standard elisp files. It apparently doesn’t handle @”\” correctly.
http://davh.dk/script/
Is a mod of zbrad’s original. Collects all the changes into a single file. It also doesn’t solve the @”\” problem. [3]
http://csde.sourceforge.net/
Based on JavaDevelopmentEnvironment. Appears to be abandonware. Not sure if this has a C# mode or not.
http://www.devhood.com/tools/tool_details.aspx?tool_id=643
this is just a link to zbrad’s code.
http://www.loveshack.ukfsn.org/emacs/index.html#cc-php-js
A modified CC mode from Emacs 22, with .elcs for Emacs 21. Done by someone who doesn’t use C#, but reported deficiencies will probably get fixed.

CategoryModes ProgrammingModes