New Page 1
Your first C# Programs
This tutorial will get you started with C# by introducing a few very
simple programs. You will
Understand the basic structure of a C# program.
Obtain a basic familiarization of what a "Namespace" is.
Obtain a basic understanding of what a "Class" is.
Learn what a "Main" method does.
Learn how to obtain command-line input.
Learn about console input/output (I/O).
Listing 1. A Simple Welcome Program: Welcome.cs
// Namespace Declaration
using System;
// Program start class
class Welcome {
// Main begins program execution.
public static void Main() {
// Write to console
Console.WriteLine("Welcome to the PSC!");
}
}
The program in Listing 1 has 4 primary elements, a namespace declaration, a
class, a "Main" method, and a program statement.
The namespace declaration indicates that you are referencing the "System"
namespace. Namespaces contain groups of code that can be called upon by C#
programs. With the "using System;" declaration, you are telling your program
that it can reference the code in the "System" namespace without pre-pending the
word "System" to every reference.
The class declaration, "class Welcome", contains the data and method definitions
that your program uses to execute. It is one of a few different types of
elements your program can use to describe objects, such as interfaces and
structures. This particular class has no data, but it does have one method. This
method defines the behavior of this class (or what it is capable of doing).
The one method within the Welcome class tells what this class will do when
executed. The method name, "Main", is reserved for the starting point of a
program. Before the word "Main" is a "static" modifier. The "static" modifier
explains that this method works in this specific class only, rather than an
instance of the class. This is necessary, because when a program begins, no
object instances exist. Every method must have a return type. In this case it is
"void", which means that "Main" does not return a value. Every method also has a
parameter list following it's name with zero or more parameters between
parenthesis.
The "Main" method specifies it's behavior with the "Console.WriteLine(...)"
statement. "Console" is a class in the "System" namespace. "WriteLine(...)" is a
method in the "Console" class. We use the ".", dot, operator to separate
subordinate program elements. It should be interesting to note that we could
also write this statement as "System.Console.WriteLine(...)". This follows the
pattern "namespace.class.method" as a fully qualified statement. Had we left out
the "using System" declaration at the top of the program, it would have been
mandatory for us to use the fully qualified form "System.Console.WriteLine(...)".
This statement is what causes the string, "Welcome to the PSC!" to print on the
console screen.
Observe that comments are marked with "//". These are single line comments,
meaning that they are valid until the end-of-line. If you wish to span multiple
lines with a comment, begin with "/*" and end with "*/". Everything in between
is part of the comment. You may place a single line comment within a multi-line
comment. However, you can't put multi-line comments within a multi-line comment.
Comments are not considered when your program is compiled.
All statements end with a ";", semi-colon. Classes and methods begin with "{",
left curly brace, and end with a "}", right curly brace. Any statements within
and including "{" and "}" define a block. Blocks define scope (or lifetime and
visibility) of program elements.
Many programs are written to accept command-line input. Collection of
command-line input occurs in the "Main" method. Listing 2 shows a program which
accepts a name from the command line and writes it to the console.
Listing 2. Getting Command-Line Input: Greet.cs
// Namespace Declaration
using System;
// Program start class
class Greet {
// Main begins program execution.
public static void Main(string[] args) {
// Write to console
Console.WriteLine("Hello, {0}!", args[0]);
Console.WriteLine("Welcome to the PSC!");
}
}
Remember to add your name to the command
// -line, i.e. "Greet Pankaj". If
// you
don't, your program will crash unless you detect and avoid such error
conditions.
In Listing 2, you'll notice an entry in the "Main" method's parameter list. The
parameter name is "args". It's what you use to refer to the parameter later in
your program. The "string[]" expression defines the Type of parameter that "args"
is. The "string" Type holds characters. These characters could form a single
word, or multiple words. The "[]", square brackets denote an Array, which is
like a list. Therefore, the Type of the "args" parameter, is a list of words
from the command-line.
You'll also notice an additional "Console.WriteLine(...)" statement within the
"Main" method. The argument list within this statement is different than before.
It has a formatted string with a "{0}" parameter embedded in it. The first
parameter in a formatted string begins at number 0, the second is 1, and so on.
The "{0}" parameter means that the next argument following the end quote will
determine what goes in that position.
This is the "args[0]" argument, which refers to the first string in the "args"
array. The first element of an Array is number 0, the second is number 1, and so
on. For example, if I wrote "Greet Pankaj" on the command-line, the value of
"args[0]" would be "Pankaj".
Now about the embedded "{0}" parameter in the formatted string. Since "args[0]"
is the first argument, after the formatted string, of the "Console.WriteLine()"
statement, it's value will be placed into the first embedded parameter of the
formatted string. When this command is executed, the value of "args[0]", which
is "Pankaj" will replace "{0}" in the formatted string. Upon execution of the
command-line with "Greet Pankaj", the output will be as follows:
>Hello, Pankaj!
>Welcome to the PSC!
Another way to provide input to a program is via the console. Listing 3 shows
how to obtain interactive input from the user.
Listing 3. Getting Interactive Input: GreetMe.cs
// Namespace Declaration
using System;
// Program start class
class GreetMe {
// Main begins program execution.
public static void Main() {
// Write to console/get input
Console.Write("What is your name?: ");
Console.Write("Hello, {0}! ", Console.ReadLine());
Console.WriteLine("Welcome to the PSC!");
}
}
This time, the "Main" method doesn't have any parameters. However, there are now
three statements and the first two are different from the third. They are "Console.Write(...)"
instead of "Console.WriteLine(...)". The difference is that the "Console.Write(...)"
statement writes to the console and stops on the same line, but the "Console.WriteLine(...)"
goes to the next line after writing to the console.
The first statement simply writes "What is your name?: " to the console.
The second statement doesn't write anything until it's arguments are properly
evaluated. The first argument after the formatted string is "Console.ReadLine()".
This causes the program to wait for user input at the console, followed by a
Return or Enter. The return value from this method replaces the "{0}" parameter
of the formatted string and is written to the console.
The last statement writes to the console as described earlier. Upon execution of
the command-line with "GreetMe", the output will be as follows:
>What is your Name? <type your name here>
>Hello, <your name here>! Welcome to the PSC!
So now you know the basic structure of a C# program. You have become familiar
with namespaces and classes. You know the "Main" method is your entry point to
start a C# program and how to capture command-line input and perform interactive
I/O.
|