C# Tutorial - 3. declare and call a method in Visual C#
Welcome to the third lesson of my Visual C# Tutorial. In this post, we are going to start with the application that we wrote through the first lesson and second. Then i would like to spend a few moments to break down every single line of code that we wrote and understand what it means. Then we are learning about declaring and calling a method.
Quick Review: we have created our very first application called "Hello World" that simply displays the message "Hello World" in the console window. We wrote two lines of code, Console.WriteLine("Hello World"); and Console.ReadLine(); inside the curly braces of method called static void Main(string[] args). Then we hit the "Start" button to run the application and looked through some possible errors that happen when you first start writing C# applications.
For the previous lessons, we have focused on the syntax that is used in building an applications with Visual C# such as capitalization, invalid token, punctuation and so on. So now we have got to build the ability to use related pre-built functionality that can be used in an C# application as it helps us deal with difficult tasks.
Let me get started by introducing you to .net framework. As you may know, Microsoft has created .net framework which is organized into two main parts. The first part is a class library. It is simply a library of codes that have been written to deal with complicated tasks such as math operations or working with text or displaying things. This library consists of a number of classes in which a wide range of functionality has been built. So we don't actually care about how it writes a message on the screen, but we can just use it to display our message.
Tips. when you find it hard to understand interrelation between class and functionality, you can just think of classes as books in a library and functionality is their contents and information. So if you know the books' titles, you can access to the information and use it.
The other part of .net framework is "Runtime". To keep it simple, it deals with interacting with computer's memory and hardware based on the codes that you as programmers declared. It is actually much more than that, but we are going to get to this later on. So now let's pay our attention to the first line of code we wrote, Console.WriteLine("Hello World");
When we write this code, we are actually calling the functionality called "WriteLine()" defined in Console class. Let's go to the .net framework library website to easily better understand. When you get there, you can find Console Class written on the main page.
As you can see, there are tons of functionality available from this class. Then you can scroll all the way down until you find functionality called WriteLine(char []). It is actually what we are calling in the application.
So, whenever you want to use pre-built functionality in .net framework class library, you should follow the syntax of calling it.
Class name + . + Functionality Name + (Parameter);
The parentheses () are actually related to methods, or functionality. When you declare or call a method, you should put parentheses () right after the method's name. In other words, when you find parentheses at the end of a code, you can just think of this line of code as a method.
Let's get back to the code and take a look at those curly braces and statements above those.
As you have learned in previous lesson 1, curly braces {} define a block of code and it has a name which is "Main" in this case. We are going get through the other keywords static and void. This kind of block of code with a name is called "Method". The basic formation of declaring a method in C# programming is as follow.
Tips. Identifier + return type + Method name + (parameter) + { code }.
We are going to talk about Identifier and return type later on, so for now we only care about the method's name and parentheses.
Tips. Parameter is through which a value passes. To put it simply, a method can use value, and it is usually passed through the parameter. In this case, WriteLine() method needs to have string value to be displayed on to the screen. So we put our message "Hello World" in the parameter's place.
As i have pointed out, a method has a name. What it really means is that a method can be called to execute the codes between its curly braces {} like we did with those two lines of code, Console.WriteLine("Hello World"); and Console.ReadLine();.
Let's take an example. As you can see, i created new class called Call right beneath Program class. Then inside the class, i wrote a method called Display() that has those two lines of code we wrote.
Then, this method Display is called by the other class "Program" as you can see on the line "call.Display();". So when you run this application by clicking the start button, the Visual Studio gets through every line of code, and when it runs into call.Display();, it will execute the codes inside the method.
Then, you will get the exact same result when you run these codes.
Post a Comment