15
Basic C#.NET Syntax
Filed Under (Learn C#) by Mystalia on 15-03-2009
Start a new windows form application like in the previous tutorial.
Drag a new button in the middle and set the text to “Off”.
Now over in the Solution explorer, right click “Form1.cs” and click View Code.
You will see a bunch of Statements, and these are lines of code. Each statement as you can see, finish with a semi-colon (;).
The code is organised into .NET Libraries, namespaces, classes and methods or functions.
The using statements at the top are .net libraries that are included, for this tutorial we won’t need any more then this, but later on we can go into special libraries to include.
A namespace is a collection of classes.
A class is a collection of methods and functions.
A method or function simply completes a task.
The advantage of being a visual studio is that we don’t have to program any user interface, we can just paint it on. At the top there are tabs (Form1.cs, start page, Form1.cs [Design]). Click the Design tab to return to the design view for this application.
Double click the button you made earlier and notice it automatically creates a new function for this button.
private means that the code can only be accessed in the current class.
void means the code will not return anything but only complete a task.
button1_Click is just a name given to associate the button to that task.
Inside the brackets below ({ and }) we will perform a task when the user clicks the button. Type “button1” and then press put a full stop, notice a window appears giving you suggested options for the button. Type “Text” (case sensative) and then press Space. We will now set the button text to something else by pressing the Equals button and then in quote marks type “Goodbye” and finish the statement with a semi-colon.
Now click the test button and then your new button and see what happens!
In the next tutorial we will see how statements can contain other statements.