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 and await feature was directly inspired by C#'s async and await 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 for if 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, and py-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.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Learn the basics of Python, including how to use Jupyter notebooks, create programs and projects, and work with strings, math, lists, loops, dictionaries, and functions.
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.
Are you new to C#, but experienced in Java? Here's a roadmap of what's familiar, and new features to learn in C#, and features in Java that aren't in C#.
New to C#, but know JavaScript or TypeScript? Here's a roadmap of what's familiar, features in C# that aren't in JavaScript or TypeScript, and alternatives for features you use that aren't in C#
We will keep evolving C# to meet the changing needs of developers and remain a state-of-the-art programming language. We will innovate eagerly and broadly in collaboration with C# developers