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.


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.

void MyFunction(int arg) 
    if (arg > 0) 

def my_function(arg):
    if arg > 0:

There are a few things to focus on:

  1. C# always uses Type Hinting, for variables, arguments, returned values…
    💡 Type Hinting - is like telling the computer what kind of data to expect.

  2. 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 readability

  3. C# 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.

  4. C# has it's own build in functions like Console.WriteLine instead of print function from python.

  5. 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 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.

// Defined Data Type
int myNum = 50;

// Non-Defined Data Type
var myNum = 50

my_num = 50
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.

FilteredElementCollector collector = new FilteredElementCollector(doc

In python on the other hand everything is much simpler.

💡 Additionally, we can still assign type hinting for variables by using (#type: …) syntax.

collector = FilteredElementCollector(doc) #type: FilteredElementCollector
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.

int myNumber = 5;

if (myNumber > 10)
    Console.WriteLine("Number is more than 10");
else if (myNumber == 5)
    Console.WriteLine("Number is 5");
    Console.WriteLine("Number is less than 5"

In python we just use colon with the right indentation

👇 Here is the same logic using python code.

my_num = 5

if my_num > 10:
    print("Number is more than 5")
elif my_num == 5:
    print("Number is 5")
    print("Number is less than 5")

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.

// for Loop
for (int i = 0; i < 10; i++)

// while Loop
int count = 5;
while (count > 0) {

# for Loop
for i in range(10):
# while Loop
count = 5
while count > 0:
    count -= 1

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.

void MyFunction()
    Console.WriteLine('Simple Function'

def my_function():
    print('Simple Function')
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.

View MyFunction(View view, int x)
    Console.WriteLine("View Name: {0}", view.Name);
    return view

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.

def my_function(view, x):
# type: (View, int) -> View
    print("View Name: {}".format(view.Name))
    return view

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.

class MyClass
    View MyFunction(View view, int x)
        // Method Body
        return view;

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.

class MyClass:
    def MyFunction(self, view, x):
        # Method Body
        return view
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!

public Wall CreateWallUsingCurve1(Document document, Level level)
    // Build a location line for the wall creation
    XYZ start     = new XYZ(0, 0, 0);
    XYZ end       = new XYZ(10, 10, 0);
    Line geomLine = Line.CreateBound(start, end);

    // Create a wall using the location line
    return Wall.Create(document, geomLine, level.Id, true

def CreateWallUsingCurve1(document, level):
    # Build a location curve for the wall creation
    start = XYZ(0, 0, 0)
    end   = XYZ(10, 10, 0)
    curve = Line.CreateBound(start, end)

    # Create a wall using the location curve
    return Wall.Create(document, curve, level.Id, True)


✅ 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!


Should I learn C#?

Should I learn C#?

What is OOP?

What is OOP?

Discuss the lesson:

🔒 Online Discord Chats are available only to Course members.

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API