Grasshopper

algorithmic modeling for Rhino

Hi

I am learning to script in c# in gh. I came across some difficulty - I cannot find any tutorials about creating classes in c#. I do not know any scripting besides processing, so I know that classes would work pretty much similiar. Can someone explain to me on some basic example how to create your own class?

There was some nice thing on aadrl2013/2014 but the website gone off.... :-(

Views: 2078

Replies to This Discussion

Er... hmm ...I would strongly suggest some proper reading (forget Internet: you can't learn programming that way):

The bible PlanA: C# In depth (Jon Skeet).
The bible PlanB: C# Step by step (John Sharp).
The bible PlanC: C# 5.0/6.0 (J/B Albahari) > my favorite

The reference: C# Language specs  ECMA-334

The candidates:
C# Fundamentals (Nakov/Kolev & Co)
C# Head First (Stellman/Greene)
C# Language (Jones)

You can define a new class using the class keyword:

[private/protected/internal/public] [static/abstract/sealed] class MyClassName

{

  // Class fields, constructors, properties and methods go here.

}

If you do not pick a scope specifier, then you'll get the default but I can never remember what the default is so I always specify a scope.

  • private means only the code inside the same scope can 'see' your class. In fact with classes this only makes sense if you define a class inside another class, so you probably never use private for classes.
  • protected means only code inside the same scope or an inherited scope can see your class. Again, protected is rare for classes.
  • internal means only code in the same assembly can see the class. This is quite common, it means you do not want anyone else to use that class.
  • public means everyone can see the class.

When a class is marked static, it may only contain static methods and fields. This means nobody ever constructs your class, all the functionality is available all the time via the class name, rather than via a class instance. Most classes are not static. A famous example of a class which is static is System.Math.

When a class is marked abstract, it means you cannot create instances of that class. Instead, the only way to use that class is to make another class which derives from the abstract class. I didn't include notation for class inheritance or interface implementations, you can read up on that online.

When a class is marked sealed, it means nobody can inherit from it. It's the proverbial end of the line.

Most often, what works well is simply:

public class MyClassName

{

 // class content here...

}

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service