Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
If you're moving from Python to C# for a new role or project, this article helps you get productive quickly. It highlights what's familiar from Python and what's different in 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
asyncandawaitfeature was directly inspired by C#'sasyncandawaitsupport. - Pattern matching: Python's
matchexpression and pattern matching is similar to C#'s pattern matchingswitchexpression. 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.
Syntax at a glance
The following examples show a few common patterns side by side. These comparisons aren't exhaustive, but they give you a quick feel for the syntax differences.
Type annotations:
# Python
name: str = "Hello"
count: int = 5
// C#
string name = "Hello";
int count = 5;
List filtering (comprehension vs. LINQ):
# Python
result = [x for x in items if x > 5]
// C#
var result = items.Where(x => x > 5).ToList();
Learn more: LINQ overview
Block scope (indentation vs. braces):
# Python
if count > 0:
print("positive")
// C#
if (count > 0)
{
Console.WriteLine("positive");
}
Class definition:
# Python
class Point:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
// C#
record Point(int X, int Y);
Learn more: Records
Key differences
As you learn C#, you discover these important concepts where C# is different from 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 forifand 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-linqprovide 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.
Tip
To learn more about C#'s type system—including class vs. struct, generics, and interfaces—visit the Type system overview in the Fundamentals section.
Finally, some features of Python 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.
Next steps
- A tour of C#: Get a high-level overview of all C# features.
- Beginner tutorials: Learn C# step by step with interactive lessons.
- What you can build with C#: Explore the application types you can create with C#.
- C# fundamentals: Dive deeper into the type system, object-oriented programming, and more.