C Sharp/Introduction

Preamble

edit

This article is a quick dive into C# for those of you who have coding experience. Don't expect to understand all of the terms and code used in this article. All that you see here will be explored in further detail in later lessons.

A Simple C# Program

edit

Below is a very simple C# application. It is a console application that asks the user to enter their name, and then prints out the message "Hello, " followed by the name that the person entered. To run this program in Visual Studio, create a project using the C# Console Application template and name it "MySimpleApplication". Then copy this code into the code file, and run the program.

using System;

namespace MySimpleApplication {
    public class Program {
        public static void Main() {
            Console.WriteLine("Please enter your name:");
            string name = Console.ReadLine();
            Console.WriteLine("Hello, {0}", name);
            Console.ReadLine();
        }
    }
}

The "using" Directive

edit

The first line of the above program is using a directive, which declares that the current file can use members of the indicated namespace without using the member's fully qualified name. Without this directive, all references to the identifier "Console" would have to be preceded by "System" and a period, because Console is a member of the System namespace. The System namespace is a part of the .NET Framework, and is a collection of the most primitive and commonly used datatypes in .NET.

All datatypes in .NET must be declared as a member of a namespace. Namespaces help disambiguate identifiers and groups related datatypes together. (Editor's note: Add link here for more information about namespaces).

Namespace Declaration

edit

The line that reads "namespace MySimpleApplication {", declares that anything declared within the curly braces is a member of the "MySimpleApplication" namespace. By convention, the root namespace of one's code should be the name of the programmer's organization (such as "namespace CompanyName.ApplicationName")or the programmer's name (such as "namespace FirstnameLastname.ApplicationName"). This helps to ensure that namespaces will stay unique. Other than the defined namespace, nothing else should reside in that area of code.

Class Definition

edit

The code within the namespace declaration defines a public class named "Program" that has a single static method named "Main". The method is the entrypoint of the application (i.e., it is the method that is invoked when the program runs). The main method may be declared with a parameter to accept an array of strings, and may return an integer value. The array of strings passed to the method represent the command line arguments used when executing the program. This program doesn't use command line arguments, and so the method wasn't declared to accept any arguments.

Body of the Main Method

edit

This is the meat of the program. The body of the method is a series of statements separated by semicolons. The first statement declares a variable of type string to store the name entered by the user.

The body makes use of two methods of the Console class, ReadLine and WriteLine. The ReadLine method accepts one line of keyboard input from the user, and returns the entered text as a string. The WriteLine method writes text to the screen, but we will elaborate more on that later. As you can see, the third statement of the program calls Console.ReadLine, and then stores the returned value into the variable "name".

The WriteLine method is actually a bit more complicated than it may at first seem, as can be seen from the fourth line of the method. The WriteLine method actually works similar to the printf function from C++. It takes a string and a variable number of additional arguments. The first argument is known as the format string. The WriteLine method searches the format string for tokens of the form {index}, such as "{0}", known as format items (format items may actually have a more elaborate syntax than what is indicated here, but that is ignored for now to keep things simple). The WriteLine function replaces each format item with the argument specified by the index of the format item. So, when Console.WriteLine("Hello, {0}", name) is called, the "{0}" is replaced by the name entered by the user.

The final line of the method is a bit of a hack that ensures that the program stays running until the user hits the "Enter" key.

Assignment

edit

Modify the body of the method so that the program asks for the person's name, asks for an adjective that describes himself and herself, and then prints a message that states "[name] is [adjective]".

See Also

edit

Where To Go Next

edit
Topics in C#
Beginners Intermediate Advanced
Part of the School of Computer Science


References

edit

Microsoft.com's C# page