In part one of this series we covered making a basic “Hello World” plugin in Mono / C#. Today we are going to dive into different types of data, and how to define them. Show me part 1! Also if you thought part 1 was ugly, I added a new javascript syntax highlighter so I recommend you read it on my blog instead of planet.ubuntu.com.
Variables
A variable is something that stores a value. In different programming languages, these are handled differently. C# is called a static / strongly typed language as you have to tell it what variables are. To explain, I will use python in comparison:
Python Example
x = "This is a string" y = 5
As you can see, we didn’t have to tell Python anything about what type of variables they are. In C# / Mono, we would do this like this:
C# Example
string x = "This is a string"; int y = 5;
I will tell you, after my years of development, that I program a ton of stuff in PHP / Python today. One of the worst and best parts of these languages is that they are dynamically typed. The fact that C# requires variables to be declared with a type before using makes life easier for testing and reading. There is a stack overflow post on what static typing gives you: Read the stack overflow post.
Variable Naming
Before we continue, you will notice in the top example I used the variable names x and y (to illustrate that the variable names really mean nothing to the compiler / interpreter. If I named them myString you might have been confused). If you do this in real life, I will find you and give you a thorough verbal lashing. I generally use camel case to name variables. Just do not abbreviate them as it really serves no purpose, and makes a lot of pain for people besides yourself maintaining code.
You can read about camel case at wikipedia.
The Source
For this program, I copied our HelloWorld.cs, and just modified it. I also have a modified makefile.
dataTypes.cs
using System;
// We create a class to contain it
class dataTypes
{
static void Main(string[] args)
{
// The type 'bool' is always true or false
bool isMonoTheOnlyPatentRisk = false;
// To check if it is true or false in program code,
// we can use an 'if statement'
if(isMonoTheOnlyPatentRisk)
{
Console.WriteLine("isMonoTheOnlyPatentRisk was set to true");
} else
{
Console.WriteLine("isMonoTheOnlyPatentRisk was set to false");
}
// Note how I named the variables -
// lowercaseFirstThenCapitalizeEachWorld
// this is called camel case -- I first read about it
// in a Charles Petzold book, but I am sure it
// probably didn't originate there. Either way its
// how I code, I recommend you name variables
// as descriptive as possible.
// Note to only kids: You will have the initial
// reaction of saying: Why should I name stuff long?
// That takes time and is stupid!
// I used to be you 10 years ago, the problem is if
// you need to use your code in several years of not
// using it. Your time will be completely wasted
// figuring out what the heck is going on, despite
// seeming so intuitive / easy to understand when you
// did it
// Integers store numbers
int ourNumber = 5;
// In most langauges we can evaluate these in
// 'if statements' like bools above, but if we try in
// C# this will fail. In C# a bool can only be 0 for
// false, 1 for true, or we can use the
// Convert.ToBoolean function like this:
if(Convert.ToBoolean(ourNumber))
{
Console.WriteLine("ourNumber was not 0!");
} else
{
Console.WriteLine("ourNumber was 0");
}
// Lets see if we can compare this number
if(ourNumber > 4)
{
Console.WriteLine("ourNumber was greater than 4");
} else
{
Console.WriteLine("ourNumber was less than or equal to 4");
}
// Make a string, and output it
string helloWorldVariable = "Hello World!";
Console.WriteLine(helloWorldVariable);
}
}
Makefile
COMPILER=gmcs all: helloWorld.exe dataTypes.exe helloWorld.exe: helloWorld.cs $(COMPILER) helloWorld.cs dataTypes.exe: dataTypes.cs $(COMPILER) dataTypes.cs clean: rm -f helloWorld.exe dataTypes.exe .PHONY: all clean
Output from our terminal
[sharms@sparrow mono]$ make gmcs helloWorld.cs gmcs dataTypes.cs [sharms@sparrow mono]$ mono dataTypes.exe isMonoTheOnlyPatentRisk was set to false ourNumber was not 0! ourNumber was greater than 4 Hello World!
Bonus Tip
Did you notice in the first part the way I named the bool? If you are programming, generally, your bool’s should start with ‘is’. This becomes very important once you start making complex ‘if statements’. Trust me on this one, it’s a habit you want.
Related posts:
#1 by Patrick on July 17, 2009 - 12:05 pm
Quote
Couldn’t agree with you more regarding naming conventions. I have never quite understood why people choose to make them cryptic in situations where they need not be.
#2 by Jon Homan on July 17, 2009 - 12:41 pm
Quote
It’d be awesome to see Part 1 with the syntax highlighter as well. Also, what’s the name of this syntax highlighter? I assume it’s a WordPress plugin??
#3 by sharms on July 17, 2009 - 12:58 pm
Quote
@Jon – Part 1 updated, and the plugin is http://wordpress.org/extend/plugins/syntaxhighlighter-plus/
#4 by Robert von Burg on July 17, 2009 - 2:29 pm
Quote
I really like these posts. I’m from the Java world, so it’s pretty straight forward for me.
#5 by Joseph James Frantz on July 17, 2009 - 9:24 pm
Quote
Steve,
I’d one up you with a similar Cobol example:) but I don’t want folks to scratch their eyes out. (Notwithstanding, I happen to love Cobol myself, despite my disparaging it). In any case this quote is great:
“One of the worst and best parts of these languages is that they are dynamically typed.”
I feel the same. it is very convenient to just start working with variables, without declaring them. On the other hand I love Cobol and other languages’ typing, and especially languages that require declaring the variable first. Too many times Ive mistyped a var, and having it declared has informed me of this on compile.
A question about C#, is it case sensitive? That’s another bugger point with me with some of these languages.
Thanks.
#6 by Kasper Henriksen on July 18, 2009 - 1:48 am
Quote
@JJF: You want your programming language to be case insensitive? Welcome to the 21st century. No new programming languages do that AFAIK.
@Author: One thing that can be odd about static typing if you come from for example Python, is how the runtime type of objects can be different from the compile time (static) type and what the effects of that are.
Here is a simple example in Java. Put in “Demo.java”, compile with “javac Demo.java” and run with “java Demo”:
public class Demo {
public static void main(String[] args) {
Inspector i = new Inspector();
Car c = new Car();
// Prints "Inspecting car: Generic car" as you'd expect
i.inspect(c);
Vehicle v = new Car();
// Prints "Inspecting vehicle: Generic car" (!)
i.inspect(v);
}
}
class Vehicle { public String toString() { return "Generic vehicle"; } }
class Car { public String toString() { return "Generic car"; } }
class Inspector {
public void inspect(Vehicle v) {
System.out.println("Inspecting vehicle: " + v);
}
public void inspect(Car c) {
System.out.println("Inspecting car: " + c);
}
}
#7 by Kasper Henriksen on July 18, 2009 - 1:51 am
Quote
wtf… I put the closing code tag at the end of it all… and wordpress decided to be stupid and just screw it up. I hate these modern piece of crap blog software that seem to think they know better than their users.
/rant
At least Java is a free form language and the compiler doesn’t give a c**p about indentation, so it’ll still work if you copy/paste it into a file.
#8 by phrostbyte on July 18, 2009 - 2:16 am
Quote
This is really good stuff man! Much more interesting to read then some political rants.
Some requests:
This might be far off. If you intend to continue this series, do some thing with GTK# and MonoDevelop. That’s really when Mono starts to shine IMO.
Show how to make a plugin for Gnome-Do. Make sure to this the open source way(tm) by picking up someone elses plugin and learning from it or modifying it to do something else. In fact thats how most Gnome-Do plugins are made!
Let’s not just stop at C#? Mono can do more. Show off the Boo language too.
Boo is a really cool CLI language and in my opinion doesn’t get enough love.
Eventually you could get into Vala too. Vala is a C# like language but it compiles directly into native code. I’m quite interested it in myself.
#9 by Steve on July 18, 2009 - 12:46 pm
Quote
So, before I dive into Mono/C#, how do I obtain RAND licence to use it legally? Noone managed to acquire it for now (withouth paying first, that is).
#10 by Kasper Henriksen on July 18, 2009 - 11:03 pm
Quote
Man… I must have been tired when I posted this. I just realized that it of course should have said
class Car extends Vehicle#11 by tc on November 29, 2010 - 3:33 pm
Quote
@Kasper Henriksen:
Actually this is all working as expected IMHO. What you’ve done in your sample is just exactly what you should not be doing in real life. The sample shows bad OO design – a method overload with 2 types from the same hierarchy. I’m wondering why would you need that? Also, isn’t this agains the Lisokv Substitution Pronciple. You are putting specialisation in Inspector which in my opinion is the mistake here. There’s a smart guy that goes by a nickname Uncle Bob that has written books on those things (start with his SOLID princilpes).
Also I’d love to see how this looks and works in Pyhon, can you please post such code with your comments. I’d love to hear what you think.