LearnRevitAPI

Learn How to Convert C# Code to Python (for Beginners)

Jan 8, 2025

Translate C# to Py

As pyRevit Hacker, it's important to understand C# Syntax. This can help you find a lot more code examples for Revit API.

And don't worry, you don't need to become C# devleoper for that. You just need to understand and recognize the main differences, so you can turn C# code into python. And it can be simpler than you would expect.

Let me show you the difference.

Biggest Difference

There are multiple differences between C# and python syntax. However, one of the biggest one is - Type Hinting.

📦 Type hinting is a way for a programmer to specify Types of your objects in code. This helps IDE recognize classes and provide you better auto-suggestion and debugging results.

Type-Hinting is mandatory in C#, as it requires you to specify types of everything in your code. In python on the other hand, it's optional. It's still possible to do, but not everyone is even aware of that.

Python vs C#

Python and C# have many differences in syntax, built-in functions and available modules. However, most of programming concepts and patterns stay the same, and can easily be translated between each other.

And when you deal with Revit API classes, then all properties, methods and how you use them stay the same. So it makes translating even simpler.

Syntax Comparison

I could write about differences all day, but it's best to show you a few examples to compare C# and python syntax. This way you can understand it right away.

Are you ready?
Then, let's compare C# and python examples.

Syntax Difference

Firstly, keep in mind that C# and Python have different syntax rules to define the scope of code.

C# uses curly braces {} to define code blocks and semi colon ; to finish statements.

Python uses indentation (spaces or tabs) to define logical code blocks and doesn't need anything to finish statements.

For example, whenever you create a function in python; all related code will be shifted to the right to define a code block.

In C#, curly braces ({}) will be used to define scope of the function and semi-colons (;) to end statements.

👇 Check Example below.


💡 This example contains a lot, so let's start from the beginning.

Variables

As you know, Variables - are used to create a container to store data that can be reused.

In C#, you need to specify the type of all variables. Even if it's as simple as int, string or anything else... There is also an option to use 'var' for undefined data-type that can change..

In python we just assign data to a variable and that's enough.

However, there is an option to add type-hinting if you want. It helps pyCharm (or other IDE) to recognize classes and provide you better auto-suggestions.

We can use #type: ClassName syntax in python in the end of a variable line like a comment.

Key Takeaways:

💡 In C# we specify Type before the variable name. And each statement is closed with a semicolon (;).

Variables - FEC Example

Here is an example for creating an instance of FilteredElementCollector with Revit API.

Python is very clear:

But in C#:
- Specify FilteredElementCollector type before variable name
- Use 'new' keyword when we use constructors
- The line ends with a semi colon ; 🤮

💡In this case, "if you remove all these extra things that C# needs, it will actually become python syntax. Did you know that?

Conditional Statements

Next, let's compare conditional statements. Let's define a number and create a few print statements.

Here is the python snippet:

Now, have a look at the same code snippet but in C#. You will notice that it has a lot more symbols and uses different function to print (Console.WriteLine)

Main Difference:

  • Type Hinting

  • Curly-Braces ({}) define scope of if statements

  • Renamed elif -> else if

  • Semi-Colons (;) at the end of statements 🤮

But overall, it's very similar.

Loops

Next, let's look at loops in C# vs python.

Firstly, here is our loops in python:

Now, let's write similar code in C#.

You will notice right away that the range function looks very foreign in C#, because it uses special syntax. But the rest is very similar.

Main Difference:

  • Range Syntax (int i=0, i<10; i++)

  • Curly-braces ({}) define code blocks.

  • Semi-Colons (;) end statements

  • Decreasing count (-=) is done differently in C# (--)

Function

Ready for the next level?

Let's compare functions. You will see 2 functions in the screenshots.

  • First one doesn't return anything

  • Second one returns some data

In python type-hinting isn't necessary. However, it's possible to add it if you want to. Pay attention to #type: (View, bool) -> View syntax. If you are a beginner, you can ignore it.

In C#, you will notice right away that it doesn't use special keyword like 'def'.

Instead it needs to use the Type of returned element before the function name. And if nothing is returned, then you need to use 'void' keyword .

💡If a function returns an element with a type of View, then you write View class name before function name.


P.S. Type-Hinting in py2 vs py3

💡 There are multiple ways to define type hinting in python especially in newer version of python. I prefer to use the method that works both in python2 and python3.

You need to create special a comment under your function name and specify types in the same order as your arguments.
#type: (arg1, arg2) -> return_elem

Conclusion

Sorry, this newsletter got out of control and became really long.

I hope you enjoyed comparing C# and Python code snippets and you realized that it's not so different after all.

Always look for patterns and you will be able to translate C# to Python yourself. You will learn it over time with practice.

Lastly, I want to provide you an example from Revit API Documentation. You can check Wall.Create method and then compare these 2 examples:

Do you recognize the differences?

P.S.

Some python users might start thinking that C# is stupid because of all curly braces and other stuff. But in reality it's just a different language, with different rules and it requires you to be more precise when you code.

Both languages have their own strengths and weaknesses, and both are widely used by developers. Everyone has their own preference depending on the goal.

However, as a pyRevit Hacker I have to share this with you, that's how many of us feel when we encounter C# code, don't we😄?


Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 7500+ people!