Open Eggbert

C Sharp

Note: The correct title of this article is C#. The substitution of the # symbol is due to technical limitations.

What is C#?

C# (pronounced "C-sharp") is a modern, general-purpose, high-level programming language developed by Microsoft. It supports multiple programming paradigms, including object-oriented, functional, and component-oriented programming.

C# Runtimes

C# code requires a runtime environment to execute. These runtimes provide services such as memory management, just-in-time compilation, and exception handling.

C# in the Web Browser

Several projects allow C# to run inside web browsers using transpilation or WebAssembly:

C# in Speedy Blupi for Windows Phone

The Windows Phone version of Speedy Blupi was written in C# 4.0, though not all language features were used. C# 4.0 was released in 2010, three years before the game launched.

Popular C# Decompilers

Decompilers allow developers to inspect compiled .NET assemblies (typically in IL - Intermediate Language) and convert them back to readable C# code.

DnSpy

A powerful .NET debugger and decompiler, now maintained as a community project.
GitHub: dnSpyEx/dnSpy

ILSpy

An open-source .NET decompiler with support for metadata inspection and PDF export.
GitHub: ICSharpCode/ILSpy

Telerik JustDecompile

A once-popular C# decompiler from Telerik. It has since been discontinued.
Archived site

Recommended C# Books

.NET Compact Framework 4.0

The .NET Compact Framework 4.0 is a streamlined version of the full .NET Framework designed for embedded systems and mobile devices with limited resources.

Key Differences from Full .NET Framework:

  1. Target Platforms:
    .NET CF targets mobile devices and embedded systems (e.g., Windows Mobile, PDAs), whereas the full .NET Framework targets desktops and servers.
  2. Size and Optimization:
    .NET CF is significantly smaller and optimized for devices with limited RAM, storage, and CPU power.
  3. API Coverage:
    .NET CF includes only a subset of the full .NET libraries. Notably:
    • No WPF (Windows Presentation Foundation)
    • Limited WCF (Windows Communication Foundation) support
    • Partial support for ADO.NET and other advanced features
  4. Garbage Collection:
    GC in .NET CF is designed to be lighter and more memory-efficient, though it may result in different performance characteristics than desktop GC.
  5. UI Support:
    Uses basic Windows Forms. WPF is not supported due to hardware limitations.
  6. Performance:
    Optimized for fast startup and low resource consumption. Code must be written with memory and CPU usage in mind.
  7. Portability:
    Applications built for .NET CF are not compatible with the full .NET Framework without modification.
  8. Development Tools:
    Visual Studio (up to 2010) with specific templates is required. Support has been phased out in modern development environments.

Summary:

Running C Sharp code in web browser

Blazor

Blazor is open source web framework, which can compiles C# to WASM and then it can run in a web browser.

External links

Bridge.NET

Bridge.NET is a open source C# to JavaScript Compiler.
Official website (dead link): http://bridge.net Archived at: https://web.archive.org/web/20210502211824/https://bridge.net/
Bridge.NET is no more under active development.
There is a fork of Bridge.NET named H5. Its Github repository is: https://github.com/curiosity-ai/h5

JSIL

JSIL is a compiler that converts .NET applications and libraries from CIL bytecode into standards-compliant JavaScript, which can run in any modern browser or JavaScript runtime. Unlike other JavaScript cross-compilers, JSIL generates JavaScript that is easy to debug while preserving the original .NET code's behavior and structure. Supporting most .NET-based languages, it works seamlessly with C# and VB.NET, with partial support for F#. The compiler handles nearly all .NET runtime features, including pointers and P/Invoke, and can translate most of the open-source Base Class Library (BCL).

Comparison of C Sharp and Java

C# and Java are similar programming languages, but have some differences.

Structures in C# vs. Classes in Java

Solution:

public class MyStruct {
   private int value;
   public MyStruct(int value) {
       this.value = value;
   }
   public MyStruct copy() { // Copy method
       return new MyStruct(this.value);
   }
   public int getValue() {
       return value;
   }
   public void setValue(int value) {
       this.value = value;
   }
}

Handling References

Solution:

public class Ref<T> {
   public T value;
   public Ref(T value) {
       this.value = value;
   }
}
public static void modifyValue(Ref<Integer> ref) {
       ref.value += 10;
   }
   public static void main(String[] args) {
       Ref<Integer> myValue = new Ref<>(5);
       modifyValue(myValue);
       System.out.println(myValue.value);  // Output: 15
   }

Object Properties

public int Value { get; set; }

To convert C# code to Java: Convert properties to methods

public class MyStruct {
       private int value;
       public int getValue() {
           return value;
       }
       public void setValue(int value) {
           this.value = value;
       }
   }

Delegates and Events

C# has delegates and events for callbacks:

public delegate void MyDelegate(string message);
public event MyDelegate MyEvent;

Java doesn’t have delegates. Use interfaces or lambdas instead:

public interface MyEventListener {
   void onEvent();
}
public class MyClass {
   private List<MyEventListener> listeners = new ArrayList<>();
   public void addListener(MyEventListener listener) {
       listeners.add(listener);
   }
   public void fireEvent() {
       for (MyEventListener listener : listeners) {
           listener.onEvent();
       }
   }
}

Nullable Value Types

C# supports nullable value types with Nullable<T> or the ? syntax:

int? nullableInt = null;

Java uses wrapper classes like Integer for nullability:

Integer nullableInt = null;

LINQ

LINQ in C# provides powerful querying and data manipulation:

var filtered = myList.Where(x => x > 10).ToList();

In Java, use the Stream API:

List<Integer> filtered = myList.stream()
                              .filter(x -> x > 10)
                              .collect(Collectors.toList());

Operator Overloading

C# supports operator overloading:

public static MyClass operator +(MyClass a, MyClass b) {
   return new MyClass(a.Value + b.Value);
}

Java does not. Use methods instead:

public MyClass add(MyClass other) {
   return new MyClass(this.value + other.value);
}

Using Statement

C# has the using statement for automatic resource management:

using (var stream = new FileStream("file.txt")) {
   // ...
}

Java uses try-with-resources:

try (FileInputStream stream = new FileInputStream("file.txt")) {
   // ...
}

Static Classes

C# allows static classes:

public static class MyUtils {
   public static int Add(int a, int b) => a + b;
}

In Java, use a class with private constructors and static methods:

public class MyUtils {
   private MyUtils() {}
   public static int add(int a, int b) {
       return a + b;
   }
}

Namespaces vs. Packages

C# uses namespace for code organization:

namespace MyApp.Utilities {
   public class MyClass { }
}

Java uses package and requires a matching directory structure:

package myapp.utilities;
public class MyClass { }

Enums

C# enums can have explicit values:

public enum Color {
   Red = 1,
   Green = 2,
   Blue = 3
}

Java enums do not support explicit values directly:

public enum Color {
   RED, GREEN, BLUE
}

Interfaces and Default Methods

C# supports default implementations in interfaces:

public interface IMyInterface {
   void DoSomething() => Console.WriteLine("Default implementation");
}

Java also supports default methods, but only from Java 8 onward:

public interface MyInterface {
   default void doSomething() {
       System.out.println("Default implementation");
   }
}

String Interpolation

C# supports string interpolation:

string message = $"Hello, {name}!";

In Java, use String.format or concatenation:

String message = String.format("Hello, %s!", name);

Exceptions

C# does not distinguish between checked and unchecked exceptions.

Java requires checked exceptions (IOException, SQLException, etc.) to be handled or declared with throws.

Dynamic Typing

C# supports dynamic typing with the dynamic keyword.

Java does not have a direct equivalent. Use reflection or Object.

Definition of multidimensional array

C#: int [1,2]

Java: int[1][2]

MAUI

.NET MAUI is cross-platform framework used to create C# apps with graphical user interface.
MAUI supports Windows, MacOS, Android, iOS (but not Linux).

External links

Official website: http://jsil.org/

Roslyn

Roslyn is .Net compiler, which also provides C# classes to analyse C# source code.
Github: https://github.com/dotnet/roslyn