Friday 31 July 2009

Beginning C# Lesson 2 - Methods and stuff

Normally when you read lots of "Learn X programming book", you can recognise a pattern in there layout, most of them start of by making a program that doesn’t do much, then explaining what a variable is and a few chapters later they explain about methods. As a student do not like that layout because I want to know how the structure of the program should look, and how it works before I start adding stuff to it.
For those who suffer short term memory loss, a general structure of a C# source code is:




This consists of what class library’s to include, the namespace, classes and the methods inside the class.
In this instance, the class “Program” has a single method inside called “Main”, this method is unique in every program because “Main” is the programs entry point. In other words, when the program has finished initialized the very first method it will execute is “Main”, if it is not there it would crash, luckily Visual Studio 2008 won’t let you compile a program without this method.

Press the Green arrow at the top of Visual Studio to debug or press F5.

Lets analyse the method “Main”:

static void Main()
“static” - Method is pre loaded into memory when the program initialises, we need the “Main” method to load up on initialisation so that it knows that there is an “Entry” point to the application, static methods can be used inside other static methods without having to create a declaration (instantiation) of them.

“void” – Methods can return data when they end, void means that when this method has finished it will not return any data

“Main” – The name of the method created, you cant have more than one name of a method declared in a class as the compiler will assume its the same method and crash, you can overload methods which will be discussed later

“()” – Methods can accept input, you can specify what variable you want to input inside the parenthisis for example (string name)
As mentioned, a Class can have more than one method inside it, let’s create a program that has more than a single method.
Create a new project called firstprogram and change the text to the following:



The class “Program” has two methods, Main and MakeAMessage.
Method “Main” writes a line of text, then executes the “MakeAMessage” method (thus writing another line of text), when the method “MakeAMessage” has no more statements, it will end and the “Main” method resumes, waiting for a user to input a character to close.
Programming is fun as there are many ways to perform the same outcome, the following source shows the same functionality as the first program but this time showing the methods returned data in a WriteLine() statement.



Before the method “MakeAMessage” was written as “static void MakeAMessage()” and now it has changed to “static string MakeAMessage()”, void changed to string.

“void” means that the method does not return any data, changing it to string means that the method now returns a “string” variable. So when the method ends it will send the “string” variable as a value to whatever method called it. In this example it sends the returned value “This is returned from the MakeAMessage method” to the “WriteLine()” method from the “Console” class.

In other words the Console writes what the returned data from the MakeAMessage method.



Methods as mentioned can also have variables entered as input as well as returning data as output,to do this we specify the data type (What type of data ie string or decimal, and what the variable will be called and referred to)and the variables name to use inside the method.



MakeAMessage(string messageText) – string is the data type as we want to enter a string of characters , and it returns the variable data from “messageText” which we declared in the methods input (also known as arguments or parameters).
Another thing to look at is the WriteLIne() method called in the Main method, it allows us to enter a string value into the “MakeAMessage” method as input.

Dont forget to practice methods, methods that return data, and methods that can allow arguments.

1 comment: