Print service provided by iDogiCat: http://www.idogicat.com/
home logo





Home > IT > Programming > C# > C# for Java Programmers

C# for Java Programmers

Overview

C# is a general-purpose, type-safe, object-oriented programming language. The goal of the language is programmer productivity. C# language is platform-neutral and works with a range of platform-specific compilers and frameworks, for Windows it's Microsoft .NET Framework. (Interpretation: C# is a copy-cat of Java.)

Object Oriented

  • all types ultimately share a common base type (like Object in Java)
  • C# does not support multiple inheritance, but it can be worked around by using interfaces.
  • function members
    • method
    • property: encapsulates a piece of an object’s state, such as a label’s text
    • event: simplifies acting on object state changes

Functional Programming Paradigm

  • Functions can be treated as values, can be passed as values to and from other functions.
  • Core to functional programming is avoiding the use of variables whose values change, in favor of declarative patterns. C# supports:
    • lambda expression: write unnamed functions on the fly that “capture” variables
    • query expression: perform list or reactive programming
    • easy to define read-only fields and properties for writing immutable (read-only) types

Type Safety

C# is a strongly typed language supporting static typing.

  • Benefit of strongly typing: type rules (whether enforced statically or at runtime) are very strict, eg. you cannot call a function expecting an integer with a floating-point argument, unless you first explicitly convert the floating-point number to an integer. This helps prevent mistakes.
  • Benefit of static typing: it enforces type safety at compile time in addition to runtime. It eliminates a lot of errors before a program is even run. It shifts the burden away from runtime unit tests onto the compiler to verify that all the types in a program fit together correctly. This makes large programs much easier to manage, more predictable, and more robust.

Memory Management

C# has a garbage collector as java, while it still supports pointers, so that in performance-critical hotspots, pointers and explicit memory allocation can be used in blocks that are marked unsafe.

Platform Support

Windows, Linux, macOS, iOS, and Android.

CLR

C# depends on Common Language Runtime (CLR), a runtime with features such as automatic memory management and exception handling. CLR is the core of Microsoft .NET Framework, and it is language-neutral, allowing developers to build applications in multiple languages (e.g., C#, F#, Visual Basic .NET, and Managed C++).

C# code is compiled into managed code, that is represented in Intermediate Language (IL). The CLR converts the IL into native code such as X86 or X64, usually just prior to execution (Just-In-Time (JIT) compilation). Ahead-of-time compilation is also available to improve startup time with large assemblies or resource-constrained devices.

The container for managed code is called an assembly or portable executable. An assembly can be a .exe or a .dll file, and contains not only IL, but type information (metadata).

IL can also be decompiled into C# with tools such as ILSpy, dotPeek (JetBrains (soon)), or Reflector (Red Gate).

A program can query its own metadata (reflection), and even generate new IL at runtime (reflection.emit).

Basics

Compiler


csc MyProgram.cs                  // produces MyProgram.exe
csc /target:library MyProgram.cs  // produces MyProgram.dll