Roadmap for Python developers learning C#
C# and Python share similar concepts. These familiar constructs help you learn C# when you already know Python.
- Object oriented: Both Python and C# are object-oriented languages. All the concepts around classes in Python apply in C#, even if the syntax is different.
- Cross-platform: Both Python and C# are cross-platform languages. Apps written in either language can run on many platforms.
- Garbage collection: Both languages employ automatic memory management through garbage collection. The runtime reclaims the memory from objects that aren't referenced.
- Strongly typed: Both Python and C# are strongly typed languages. Type coercion doesn't occur implicitly. There are differences described later, as C# is statically typed whereas Python is dynamically typed.
- Async / Await: Python's
async
andawait
feature was directly inspired by C#'sasync
andawait
support. - Pattern matching: Python's
match
expression and pattern matching is similar to C#'s pattern matchingswitch
expression. You use them to inspect a complex data expression to determine if it matches a pattern. - Statement keywords: Python and C# share many keywords, such as
if
,else
,while
,for
, and many others. While not all syntax is the same, there's enough similarity that you can read C# if you know Python.
As you start learning C#, you'll learn these important concepts where C# is different than Python:
- Indentation vs. tokens: In Python, newlines and indentation are first-class syntactic elements. In C#, whitespace isn't significant. Tokens, like
;
separate statements, and other tokens{
and}
control block scope forif
and other block statements. However, for readability, most coding styles (including the style used in these docs) use indentation to reinforce the block scopes declared by{
and}
. - Static typing: In C#, a variable declaration includes its type. Reassigning a variable to an object of a different type generates a compiler error. In Python, the type can change when reassigned.
- Nullable types: C# variables can be nullable or non-nullable. A non-nullable type is one that can't be null (or nothing). It always refers to a valid object. By contrast, a nullable type might either refer to a valid object, or null.
- LINQ: The query expression keywords that make up Language Integrated Query (LINQ) aren't keywords in Python. However, Python libraries like
itertools
,more-itertools
, andpy-linq
provide similar functionality. - Generics: C# generics use C# static typing to make assertions about the arguments supplied for type parameters. A generic algorithm might need to specify constraints that an argument type must satisfy.
Finally, there are some features of Python that aren't available in C#:
- Structural (duck) typing: In C#, types have names and declarations. Except for tuples, types with the same structure aren't interchangeable.
- REPL: C# doesn't have a Read-Eval-Print Loop (REPL) to quickly prototype solutions.
- Significant whitespace: You need to correctly use braces
{
and}
to note block scope.
Learning C# if you know Python is a smooth journey. The languages have similar concepts and similar idioms to use.
Samarbejd med os på GitHub
Kilden til dette indhold kan findes på GitHub, hvor du også kan oprette og gennemse problemer og pullanmodninger. Du kan få flere oplysninger i vores vejledning til bidragydere.