M2-02
python vs C# Differences
Let's have a brief comparison of C# syntax to python syntax.
Pause the video to compare snippets if you want to learn more about them.
Summary
Programming language similarities
All programming languages share many similarities.
While the syntax and some unique features may vary, the core logic remains the same. This makes it relatively easy to translate from one language to another, as long as we have a good understanding of the syntax.
So let's start by comparing C# and Python syntax, which will make it easier to translate real-world examples for the Revit API in the upcoming lesson.
Code Scope
Let's start with a basic overview in C#(left) and Python(right).
I will cover If Statements and Functions later, so let's focus on obvious differences first.
There are a few things to focus on:
C# always uses Type Hinting, for variables, arguments, returned values…
💡 Type Hinting - is like telling the computer what kind of data to expect.Python uses colons (:) and indentation (spaces or tabs) for defining code blocks, whereas C# relies on curly braces {}
💡 Spacing and line breaks in C# doesn't matter, but it's good to keep it consistent for readabilityC# uses semicolons (;) to end statements while in python statements are placed in a single line, unless \ symbol is used to break a line into multiple.
C# has it's own build in functions like Console.WriteLine instead of print function from python.
There are different naming standards of variables and function between C# and python.
Python users use snake_case: use_of_underscores_to_separate_our_words
C# users use CamelCase - WhenEachWordStartsWithCapitalCase
Let's go step by step through main programming concepts to understand the difference.
Variables
Variables are used for storing our data.
In python we can dynamically define variables and python will assign a data type based on the actual data automatically. That's a great help for beginners.
If you assign an integer, it will know that this variable has a type of int, if you assign text it will know it's a string…
However, in C# we must manually define these data types by indicating the type before the variable name. And the statement should end with a semi colon (;)
Optionally, we can use 'var' keyword to create an undefined data type for variables, meaning that they can hold various data types.
Revit API Variables Example
Let's have a look at the common example of FilteredElementCollector (FEC for short).
In C# we specify in the beginning of a variable collector that it will have a data type FEC, and then we end the statement with semi-colon.
Also you might noticed keyword 'new'. It's used to declare that we are creating a new instance of a class by using its constructor method.
In python on the other hand everything is much simpler.
💡 Additionally, we can still assign type hinting for variables by using (#type: …) syntax.
If Statements
Let's explore conditional statements.
First of all C# uses else if instead of pythonic elif.
Then you can clearly see how C# uses curly braces for defining code blocks for each conditional statement.
In python we just use colon with the right indentation
👇 Here is the same logic using python code.
Loops
Here are examples of creating loops in C#. Similar to Python, we can make 'for' and 'while' loops.
The syntax is mainly looks different due to a range function syntax. I won't go deep into that, but you can get the main idea by comparing it to python.
Functions
Let's have a look at function in more depth.
In python we use def keyword to begin defining our function, while in C#, we must define the Type of the returned value. If a function does not return anything, then we use the 'void' keyword.
👇Here is the most basic function example we can create.
Functions with arguments
in C#, If we want to include arguments in a function, then we need to provide type hinting for each argument as well.
If a function returns an element, we must specify Type of that element in front of a function instead of using void keyword.
In python, Type Hinting is not required, however I would encourage you to use it in reusable functions. That will help you in the long run to maintain your code.
Class Methods
Lastly, let's have a look at Class methods.
As you are aware, methods are functions within classes.
And in C# it works exactly like functions.
The biggest difference between C# and python in methods is additional self argument.
In Python's OOP, self signifies an instance of the instantiated class. I won't dive deep, assuming you're already familiar with this concept.
Create Wall Method
Lastly, let's combine all of that by looking at one of the examples in Revit API Docs.
👀 Don't spend too much time on it, as we will look at it in the next video!
HomeWork
✅ Compare syntax examples between C# and Python in this lesson.
👀 We are going to go through Revit API Docs and translate a few C# examples to python in the next Lesson!
💡 Remember, you don't need to learn C# to do that! Just get familiar with it.
⌨️ Happy Coding!
Questions:
Should I learn C#?
What is OOP?
Discuss the lesson:
🔒 Online Discord Chats are available only to Course members.