Categories
Entertainment Uncategorized

Human Voronoi

Categories
Uncategorized

Why learning code is so hard!?

Come across this article and find it very true.

https://www.thinkful.com/blog/why-learning-to-code-is-so-damn-hard/

 

Categories
Uncategorized

From Python to C#

Currently, my main language is C#. However, two year ago I switched from python to C#. The journey was not easy. I hope this post could be a kick off if you decided to switch from python to c# or simply to understand the c# code.

Firstly, why both language? Each programming language has its own strength and weakness, they are applicable to different uses and different context. So far, in the architecture and engineering field, the common languages I encountered are python, c#, vba and java. I only know python and c#. Java is very similar to c# so I wont cover here.

Python is a dynamic language while C# is an object oriented language. I will not go into details here. (If you wish to find out more, you can take a look at this blog post – Java is very similar to C# too as it is an object oriented language. https://www.activestate.com/blog/2016/01/python-vs-java-duck-typing-parsing-whitespace-and-other-cool-differences).

Python is obviously easier to learn and easier to write. It’s a great friendly language for beginners to programming. Besides, python has a lot of convenient functions, methods for you to play around with numbers. There are also a lot of cool python libraries out there for machine learning or computer vision. On the other hand, C# is much harder to read and harder to write.  Object oriented languages are good to develop software as they’re meant to be more difficult to demand the programmer to be more disciplined and write smarter code. It offers means for the programmer to structure their complex code better so different people can work on the same project (programming project), or help you to improve your own code later when you have not work on the same project for a long time. C# programs also run faster with better performance. If you understand c#, you can also understand the APIs or SDK of the existing softwares better.

Anyway, I list few main pointers of C# that could possibly help you kick start your C#

Syntax

In python, the hierarchy is noted by indentation. In C#, it is through character.

Type, type, type

The biggest difference between python and C# is that you need to declare type in C# !

In C#, everything you write down has a type. Think of it as a category. For example, in real life, cactus is plant, tiger is animal, Sophie is human etc. Similarly, in C#, 1 is an int, 1.0 is a double etc.

In RhinoCommon, you have different types: For example: Curve, Brep, Point3d

You can declare type by writing down the type before the name.

int a = 0;
Point3d pt = new Point3d(0.0,0.0,0.0);

For example:

a is an int and it is equal to 0.

pt is a Point3d and it is made up of 0,0,0 coordinates. The word new note that you are creating a new object.

For common value type such as int, double or boolean you can straight away right down the value. Most of other custom objects (that mean the objects belong to the software, like point3d, curve etc. they are RhinoCommon object) you will have to write new “type” when you create them.

If you do not specify the type( for example: a = 0), it will reports ‘a does not exist in the context’. If you do not write new, it will report: ‘Rhino.Geometry.Point3d’ is a ‘type’ but is used like a ‘variable’

Many times, you will see a type called var. var means variable, but it just means it could be anything. It’s just a habit for neater writing. I can write

var a = 1;
var b = new Curve();
var c = new Point3d(0,0,0);

or I can write

int a = 1;
Curve b = new Curve();
Point3d c = new Point3d(0,0,0);

They mean the same thing

Declare list – list vs array

List is a foreign concept to python. In C#, there is array and there is list.  In python, everything is an array.

Difference? A quick google search will give you something like this

“An array is an ordered collection of items, where each item inside the array has an index.”

“List is a collection of items (called nodes) ordered in a linear sequence.”

==> sounds like the same thing. Well, the difference mainly is mainly behind the scene. List and array have different ways of storing the memory. Array is like having a series of boxes then putting in the items inside. List is a collection of the items. Well, we can ignore all these nerdy differences for now.

I will introduce C# List syntax below and compare it with python array.

C# list

List<int> list = new List<int>();
list.Add(2);
list.Add(3);

Python array

arr = []
arr.append(2)
arr.append(3)

Note the difference in the way we declare list and array. In C# you have to write List and specify the type of them items inside <>

For loop

This is mainly syntax. There are two ways to declare for loop

For loop in python

for i in range(0, 10):
for pet in pets:
for (int i = 0; i < 10; i++)
        {
        }
List<string> pets
foreach (string pet in pets)
        {
        }

the first one: for int i = 0, we start item i of type integer, loop it till 9 (less than 10), i++ means keep continue add 1 to a new loop.

the second one: for each item (which of type string  and name pet)in the list named “pets”.

Overall

This post is no way a full guide to object oriented language C#. The more I know about C#, the more I realize the difference between C# and python is not merely syntax, but more of the concept, about the way the language communicate with the computer. I have seen some different codes out there. A good programmer understands the language concept very well and knows how to use them to the way he wants to structure his code. Object oriented language is more complex and requires more patience to learn. There is a big difference between a code that can run and a code that runs well. If you wish to know more, you can find a comprehensive course to object oriented language. However, I hope the post could help you write and debug simple code, or the least to read other people’s code