C# Tutorial - Part I (C# as a Language)
Contents
Part I - C# as a Language
I'm going to quickly move through some of the most basic programming concepts. Chances are, if you're reading this, you have some basic understanding of another programming language or at least just programming in general. If you do not, then check out this generic programming tutorial before reading on.
If you've had any experience with even a small amount of programming then you should know what a variable is. It is a piece of code that stores some type of data. For instance, an int
is short for "integer" and a variable of type int
is a positive or negative whole number.
int myVariable = 3;
The above code shows how to declare a variable and assign it a value in C#. It is important to note that you do not have to assign the variable when you declare it.
int myVariable;
I have just declared a variable named "myVariable" and its current value is NULL or zero. NULL means it has no value, the variable does not contain any data. In the case of an int, it is a non-nullable type which means it cannot be NULL and is set to zero by default instead. You can then assign values to a declared variable later in your code.
myVariable = 5 + 2;
myVariable
would be equal to 7 in this case. Notice that programming statements in C# always end in a semi-colon. Statements are usually a line of code that performs one small task (like assigning a value to a variable). Another concept you should be aware of are block statements. Block statements are blocks of code statements to be executed. In C# these are defined by enclosing the inner-statements within curly braces.
void MyFunction()
{
int myVariable = 7;
myVariable = myVariable + 2;
}
The above code defines a function (known in C# as a "method") wherein a variable called "myVariable" is declared and assigned a value of 7. It is then re-assigned a value of itself + 2, which would be 9. Everything within the curly braces is said to be "in that scope". What this means is that the variable myVariable only exists within the scope of that method. Once execution of the code leaves that method the variable is no longer in scope and the memory will be freed.
int myVariable = 7;
void MyFunction()
{
myVariable = myVariable + 2;
}
This code performs the same function as the previous snippet except the variable myVariable is no longer in the scope of MyFunction(). It is now in what is known as "global scope". It is declared on the same level that the method is declared. If other methods were declared also, myVariable would be available to them as well. Another block statement you may have heard of already is the IF statement.
void MyFunction()
{
int myVariable = 7;
if (myVariable == 7)
{
myVariable++;
}
}
There are a couple things going on here. First, the IF block statement is an important fundamental part of any programming language. Try reading it like English: "If variable called myVariable equals the value 7, then do this". The code statements within the IF block will only be executed if myVariable contains 7 as its value. Next, the statement within the IF is actually just a shorthand way of saying myVariable = myVariable + 1;
. Similarly myVariable--
would be the same as myVariable = myVariable - 1;
. Just much less typing.
IF statements allow you to define any number of paths for the code to follow using else if
or simply else
. For example:
if (myVariable == 7)
{
// Do something
}
else if (myVariable == 8)
{
// Do something else.
}
else
{
// Do something entirely different.
}
You can only have one starting if
and one else
but you can have as many else if
s as you need. The ability to use an IF statement can be one of the first milestones in learning programming as it is the first real time that you create logic paths based on data in your program.
There are a lot more fundamental concepts I could go into regarding generic programming as it relates to C#, but for the purposes of this tutorial I'm going to stop here and jump into some more defining aspects of the language.
C# is an object-oriented language (OOL). If you don't know what that means it's okay, starting out on the road toward becoming a programmer you will encounter a lot of unique concepts. At first they can be daunting, but if you stick with it learn about them one by one then you'll be surprised how quickly you come to understand them. An object-oriented language is a language that groups common sets of code into objects. To give you an example let me write up some quick pseudo-code. Let's say that your client sells televisions and wants some software allowing him to add in and store information about different types of televisions. A pseudo method you create to store a new TV in the database might look like this:
public void AddTelevision(int ID, string model, int size, decimal price)
{
database.Write(ID);
database.Write(model);
database.Write(size);
database.Write(price);
}
This can be very tedious work. When you have common sets of data and you constantly need to pass those sets of data around your application it can be hard to keep track of all the variables. Imagine if you had to track 50 pieces of information about a TV. That would mean that you have to create 50 variables of various types and pass them around your application.
Welcome to the 21st century! Here in the future we don't create swaths of variables and just throw them around like children in a sandbox. Instead, we define objects and we attach relevant data to them. We then pass the objects around our application as if it were a single variable. In order for me to explain an object I first need to explain a "class". A class is a block of code that contains methods, variables, and properties (which you will learn about shortly). There are two types of classes, static and non-static. A static class is the easiest to understand. When a class is declared with the static
keyword it means that the methods in this class are not part of an object and can be called anytime. For example:
public static class MyClass
{
public static string MyMethod()
{
return "Hello World!";
}
}
This is a class containing a method called MyMethod. MyMethod returns a string variable (a variable containing unicode characters) with a value of "Hello World!". Another class elsewhere in the application could call this method just like this string message = MyClass.MyMethod();
. Notice that the method is also declared with the static
keyword. Static classes can only contain static members. So any methods or variables within the class must also be static. Don't worry too much about the public keyword for now; just know that it is what allows the class to be called from elsewhere in the application. If it were "private" then other classes could not call it or it's members.
So what is a non-static class? A non-static class is a class that can be instantiated. A non-static class can contain something that a static class cannot: properties. Here is an example of a non-static class:
public class TV
{
public int ID { get; set; }
public string Model { get; set; }
public int Size { get; set; }
public decimal Price { get; set; }
public string MyMethod()
{
return "Hello World!";
}
}
If you were wondering what I meant when I said a class like this could be instantiated then take a look at this next line of pseudo-code.
TV sonyTV = new TV();
sonyTV.ID = 123;
sonyTV.Model = "LCD Blackbird";
sonyTV.Size = 52;
sonyTV.Price = 7.44m;
database.Write(sonyTV);
Did you see what we did there? We created an instance of the TV class. We then assigned values to its properties. Properties are just variables attached to a class. Using this technique I could create as many different instances of the TV class as I wanted. "sonyTV" is the name of the TV object we just created. I could create another instance of TV with a different name if I wanted. Then I would have two TV instances in memory. Once I have my TV object I can then pass it all over the application. In the pseudo-code I passed it to a fictional database.Write()
method which would theoretically know how to save a TV object to the database.
When you have an object you can access its public properties very easily:
string modelName = sonyTV.Model;
How awesome is that? Object-oriented programming makes life so much easier.
Code Comments
One other topic I'd like to touch on real quick is the concept of code comments. Comments are lines of text that are ignored by the compiler and are only there for the programmer to see. You can comment an entire line with two forward slashes or entire blocks of text with a forward slash and an asterisk then an asterisk and a forward slash at the end of the comment. For example:
public void MyMethod()
{
// Do stuff here later.
}
or
public void MyMethod()
{
/* This is a longer comment that
takes up more than one line. So
rather than put two forward slashes
in front of every line I just used
a block comment. */
string myString = "some value";
}
I know this tutorial has jumped around a little bit, but I wanted to cover some of the important basics before we get to the good stuff. In part II of this tutorial I will introduce you to your first program :)
What You Just Learned
- C# uses many concepts similar to most other popular programming languages.
- C# is an Object-Oriented language.
- Objects are the basis for life, the universe, and everything.
- How code comments work.