Introduction to Programming in C#

Why are we using C# in this course?

  • Grasshopper models are inherently limited in size: large models are difficult to navigate/search, limited flow of control, limited debugging capabilities
  • We will develop C# plug-in components for Grasshopper/Rhino. This allows us to  combine the benefits of visual and object-oriented programming.
  • Reuse built-in libraries in C#: data structures, algorithms, input/output …
  • Development facilitated by Integrated Development Environment (Visual Studio): code inspection at run-time, search code, …
  • Learning an object-oriented programming language may be initially more difficult than a scripting language, but this is offset by better flexibility and scalability.

What is C#?

  • C# is a general-purpose, object-oriented programming language.
  • Part of Microsoft .NET application development framework: more than 4000 classes for data input/output, data processing, networking, …
  • Other programming languages in Microsoft .NET framework: F#, VB.NET
  • ISO standard.
  • Most recent version: 7.0, released 2017.
  • Object-oriented: code organized around classes (objects).
  • Object-oriented programming languages: Java, C++, Python.
  • Programming languages that are not (or not fully) object-oriented: C, Javascript.

How to approach programming?

  • Be patient – don’t get too frustrated and discouraged by errors
  • Learn from others (code reuse, documentation, online forums)
  • Programming can be fun!

C# program structure

Code example: print ‘Hello World’ to output.


Compilation errors

  • Source code is processed by the compiler.
  • A compiled program is executed only after successful compilation.
  • Compilation error checker provides visual and text feedback to help find errors.
  • Strategies to minimize compilation errors: clean formatting of code (use indentation, avoid long lines of code, use white space to group related code).
  • A successfully compiled program may still cause run-time errors.

Code example: compilation errors


  • Many classes are already included in C#.
  • Namespaces are a way to organize these classes and to use them.
  • ‘using …’ directives facilitate access to classes.
  • Auto-completion supports writing code based on namespaces.

Code example: namespaces



  • A variable is an identifier that points to a location in memory which stores a value.
  • A variable has a type.
  • A variable may change its value over time.
  • Variable types have methods, and operators can be applied to variables based on their type.

Code example: variable types

Code example: increment/decrement operators

Code example: type fields and methods


Collections: Arrays

  • Most basic collection
  • Typed (all data items must have same data type)
  • Ordered
  • Duplicates: an item may appear multiple times
  • Static: fixed size – modify items, but unable to add/remove items

Code example: arrays


Collections: Lists

  • Based on array
  • Frequently used collection
  • Dynamic: add, remove items

Code example: lists



  • Methods are code blocks that include statements.
  • Methods may be called by the ‘Main’ method, other methods, etc.
  • A method has a signature that consists of access level, name, parameters, and return value.

Code example: number comparison