Beginner's Guide to C# in Grasshopper

Beginner’s Guide to C# in Grasshopper

Welcome to the Beginner’s Guide to C# in Grasshopper! In this guide, I’ll introduce you to C#, a powerful programming language that is commonly used in AEC industry. Microsoft uses this language for almost all their products and is becoming more and more popular among engineers and architects. Imagine Grasshopper as a platform where you can construct programs by connecting colourful blocks. Think of C# as the magical text-based language that takes these blocks to the next level. If you’re new to all of this, don’t worry – this guide is designed to be your starting point on the path to becoming an Engineer 4.0.

Table of Contents

Sign up for free mailing and you will receive a valuable piece of knowledge about Grasshopper every week.
Already more than 8,000 engineers study with me and take full advantage of the power of Parametric Design.

Understanding C# and the .NET Framework

C# (pronounced “see-sharp”) is like a wand that brings computer programs to life. It’s a language that gives step-by-step instructions to computers. Think of it this way: imagine having a special toolbox called the “.NET Framework.” Inside this toolbox are tools that give us the possibility to build various types of applications, including desktop and web applications, among others. The beauty is that these tools come pre-made, so we don’t have to start from scratch whenever we want to create something. They save us an immense amount of time and effort.

Platforma .NET Framework to skrznka z narzędzimi, którą wykorzystuje C#
.NET Framework is a toolbox that C# uses.

The .NET Framework was developed by Microsoft in 2002 to simplify the creation of diverse software applications. Here are some familiar examples of applications and technologies built on the .NET Framework:
– Microsoft Office Suite:
– Microsoft Azure:
– Visual Studio

It means that C# language will be developed in the future. Microsoft will not just drop working on this, so it is a good investment in learning this language. It is reliable and opens new doors if you already know Grasshopper.

Why C# in Grasshopper Matters

The C# scripting component is integrated within Grasshopper and has a similar interface to that of other typical components. They can read input and produce output, and have an editor to write custom code with access to RhinoCommon. They are used to create specialized code and workflows not supported by other Grasshopper components. You can also use them to simplify, optimize and streamline your definitions by combining multiple functions. C# scripts run exceptionally fast, breathing life into your designs quickly and reducing the complexity of Grasshopper workflows.

When you’re writing C# code, the software lends a helping hand. It offers suggestions on what to write next, making it especially beneficial for beginners. If you aspire to craft special tools for Grasshopper, known as GHA assemblies or plug-ins, C# is your ticket to unlocking a new realm of creativity.

Komponent C# w Grasshopperze
C# component in Grasshopper - Additional component

C# Challenges

Now, let’s be real. C# isn’t the easiest programming language to master. It’s slightly less forgiving compared to other languages, like Python. This means you need to understand the data you’re working with more deeply. This might lead to longer code segments as clarity takes precedence. However, rest assured that this approach makes you a better programmer and minimizes the chances of errors.

Excited to start on the C# journey within Grasshopper? Lets start with some basic of C# programming inside Grasshopper

C# in Grasshopper component

You can find the C# scripting tool in the Math tab under the Script section. To add it to your canvas (work area), just double-click and type “C#”.

When you first add it, you’ll see two inputs labeled ‘x’ and ‘y’, and two outputs labeled ‘out’ and ‘A’. The ‘out’ output is where any errors or important info about your code will show up. If you connect the ‘out’ output to a Panel tool, you can easily read info.

There are two kinds of messages that appear in the ‘out’ output:

1. Compile-time messages: These are like notes from a checker that looks at your code. They can point out mistakes or things you should fix.

2. Runtime messages: You can put your own notes in the ‘out’ output while your code is running. This helps you understand what’s happening inside your code.

The names you see on the inputs and outputs (except ‘out’) are like labels. They’re also the names of things inside your code. So if you change them, remember to follow the rules for naming things in C#. When you zoom in, you’ll see plus and minus signs (Zoomable user interface like Merg component). These let you add or take away inputs and outputs as you need.

Komponent C# w Grasshopperze
C# component in Grasshopper

Data types - C# in Grasshopper

Primitive types refer to the basic and built-in types provided by the programming language.
The following examples declare variables of primitive data types:

Typy danych pierwotne - Źródło: Przewodnik niezbędny do skryptowania w C# dla Grasshoppera autorstwa Rajaa Issa
Primitive data types - Source Essential guide to C# Scripting for Grasshopper by Rajaa Issa

C#, you declare string values using double quotation marks (“”) instead of single quotation marks (”). Single quotation marks are typically used to denote individual characters (char values), whereas double quotation marks are used for strings.

Here’s how you declare string values in C#:

Showing additional operation results

There are some custom data types in Rhino which were designed specifically to represent geometry. Some of the examples are Point3d, Line, Poyline, Brep.

Typy danych w komponencie C#
Data types in C# component

Note: outputs types are always set to generic „object” type.


In addition, you have three ways of providing data (item, list, data tree).

Dostęp do danych wejściowych. Pojedyncza wartość (Item), lista (List), drzewo danych (Tree).
Input data access. Item, List, Tree

Defining Variables in C#

In C#, the process of defining a variable involves specifying its data type followed by the chosen variable name. Certain guidelines must be followed to ensure uniqueness and clarity in your code:

– Variables must avoid reserved keywords. for example bool, class, char, double
– They should begin with a letter or an underscore._count, firstName, totalAmount
– Permissible characters: Letters (uppercase and lowercase): A to Z and a to z, Digits: 0 to 9, Underscore: _
– Whitespaces are not allowed.

A declared variable essentially acts as a container for storing specific data types. The initial assignment of a value to a variable is referred to as its initialization.


Useful Tips:
1. Descriptive Naming: Opt for descriptive variable names that provide insight into their purpose. For instance, “userAge” is preferable over “var123”.
2. Camel Case Convention: Adhere to the widely adopted camel case naming convention in C#. This implies starting with a lowercase letter and separating words using uppercase letters. For instance, “totalPurchaseAmount”.

By adhering to these guidelines and examples, your code will not only be plagiarism-free but also maintain readability and conformity with best practices in C# programming.

Comments for C# Code Clarity

Code comments are like notes you write directly into your code that the computer ignores when running the program. They’re useful for explaining tricky parts of your code. There are two main types of comments: short ones and long ones.

Comments are really handy because they help you and others understand your code better. They act as reminders for you and explanations for anyone reading your code. To write a comment, you can use two methods:

1. Single-Line Comments: You just put two slashes (//) before it, and the computer won’t pay attention to it. In Grasshopper’s C# code editor, comments show up in green.

Comments in c# script
Single-line comments in c# script

2. Multi-Line Comments. If you want to explain something longer, you can enclose it between /* and */, like this:

Comments in c# script
Multi-line comments in c# script

Using comments like this makes your code easier to understand for everyone.

Worth to remember!

All instructions in C# have to end with a semicolon (in general).

Grasshopper Exercises

Now you gain basic theoretical knowledge, and you are ready to apply it in practice. Download the Grasshopper File and solve the exercises that I have prepared for you. Under every single exercise, you will find instructions on what you should do. On the left from the exercises, you will find a cluster with a provided solution.

C# In grasshopper
Learn C# in Grasshopper - Exercises

If you want to get more information about Grasshopper and learn parametric modelling, download the free guide – FREE DOWNLOAD

Grasshopper is a plugin to Rhino that you can download HERE. 

Did you like that post ? Share it with others !

We spend a lot of time and effort creating all of our articles and guides. It would be great if you could take a moment to share this post !

Share:

Comments:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Author:

Download BIM CASE STUDIES:

After reading this guide you will learn:

  • How BIM is used on the biggest projects in Norway
  • What were the challenges for the design team and how were they solved
  • What were the challenges on the construction site and what was our approach to them

Newest articles: