Getting Started with CSharp

From IPRE Wiki
Jump to: navigation, search

This page is designed to help get started with CSharp. There are two main ways to use CSharp: with Microsoft Visual Studio, and without. If you are in a non-Windows environment, you can compile and run CSharp code using Mono.

Using Microsoft's Visual Studio

Microsoft offers a Visual Studio Express edition for free use. Get the version for C#:

To get started, try:

Using Mono

Mono is an open source implementation of the CSharp standard. You can download Mono from here:

Mono has a development environment, called monodevelop. You can use that, or standard command line tools.

You can compile a CSharp program using gmcs:

gmcs Program.cs

which will produce a file named Program.exe which you can run with mono:

mono Program.exe

Hello World

CSharp is syntactically very similar to Java. Here is a HelloWorld.cs:

using System; // Like "import" in Python; for Console

public class HelloWorld {
    public static void Main() {
        // This is a single line comment
        /* This is a
           line comment 
        Console.WriteLine("Hello World! From Softsteel Solutions");

Differences between Mono and Visual Studio

There are some differences between Mono and Visual Studio, but fewer every year. Many of the standard CSharp features will run on either, including Microsoft's graphics, called Windows Presentation Foundation or WPF for short.

CSharp for DLR

If you want your CSharp objects to be useful in DLR languages (like IronPython), then check out:

Dino says:

Srivatsn's blog is an important piece of the puzzle, but the more general answer is implementing the __*__ method directly should always work. If it doesn't then it's a bug - like where __repr__ wasn't working w/o implementing the interface until recently.

But there is a good reason to not implement the __*__ method - and that's ensuring that your objects will work good in a multi-language environment. What IronPython does to expose .NET objects into Python is it maps a large number of .NET interfaces and methods into Python methods. If you'd like to see the all of the mappings they're contained entirely in TypeInfo.cs. So instead of implementing __getitem__ you can implement a C# indexer, instead of __enter__/__exit__ you can implement IDisposable - assuming you're not doing more interesting things w/ __enter__/__exit__, etc... If there's some mapping that you think should exist but we don't have let us know - for example until recently we had overlooked mapping IDisposable.

To get the best interop if there's a .NET interface or operator method that maps onto the Python methods you should use that. That includes the extended operators that we've defined for the DLR default binder like GetCustomMember as Srivatsn's blog demonstrates. Otherwise fallback to the __*_- method.