Home > Articles > Programming > C#

Getting Started with C#

  • Print
  • + Share This
Joe Mayo's C# tutorial guides you through the process of creating a simple, minimal C# program and exploring its features.
This article is excerpted from C# Unleashed .
Like this article? We recommend

Like this article? We recommend

This article starts by creating a simple, minimal C# program that will give you a feel for what C# looks like and be a stepping-stone to more features. This article includes instructions on how to compile this program. When needed, additional compilation features will be presented where they're used.

This article also provides coverage of the C# data types and tells about the various types C# provides. There will be plenty of examples of declarations and the kinds of data that can be stored. The article also covers the relationship between types and how to make conversions. It finishes by showing various ways to provide input and output for programs.

Writing a Simple C# Program

Let's dig in. For this first program, Listing 1 shows the near-minimal amount of code necessary to write a C# program. When it executes, it will print the words "Howdy, Partner!" to the console.

Listing 1: A Simple C# Program

 1: /* 
 2: * FileName: HowdyParner.cs
 3: * Author:  Joe Mayo
 4: */
 6: // Program start class
 7: public class HowdyPartner
 8: {
 9:    // Main begins program execution
10:   public static void Main()
11:   {
12:        // Write to console
13:     System.Console.WriteLine("Howdy, Partner!");
14:   }
15: }

Line 7 of Listing 1 shows a class declaration. Classes are what C# uses to declare or define objects. They describe the attributes and behavior of an object. An object is anything that has attributes and behavior. While classes are definitions, objects are the actual entities that exist when the program runs. There can normally be many objects based upon a single class declaration. Objects are the building blocks of the C# programming language. C# is an object-oriented programming language; therefore, it has a starting object. That's what this class represents—the starting object of this program.

For C++ Programmers

The Main() method is located inside of a class instead of by itself. Also, the spelling of the first letter is capitalized.

For Java Programmers

C# is case sensitive. The spelling of the first letter of the Main() method is capitalized. Also, the C# Main() method can declare return types of both int and void.

The identifier, or name, of a class follows the class keyword. This class is called HowdyPartner. Classes can have almost any name, but, whatever the name, it should be meaningful. Details of identifiers and keywords are described in a couple more sections.

Left and right braces indicate the beginning and ending, respectively, of a block. In Listing 1, the beginning of the class block starts on Line 8 after the HowdyPartner identifier, and the end of the class block is on Line 15, by itself. In C#, it's common to begin and end portions of programs with curly braces.

There's a method declaration on Line 10. This is the Main() method. Every program has a Main() method that is the starting point for the program. When the program begins, this is the first method called.

There are a couple identifiers in front of the Main() method. C# Main() methods always have a static modifier. In C#, there are two types of objects—static and instance. Instance objects are formally declared, and there can be many of the same type with different attributes. However, there can be only a single copy of a static object in existence for a given program. The only time that you can execute a method without an instance of its containing object is if that method is static. Because no instance of the starting class exists as an object when the program starts, a static method must be called. This is why the Main() method is static.

The other identifier is void. This is actually the Main() method's return value. Return values are useful for returning program status to a calling program or utility when a program exits. When void is specified, the method does not return a value. In this case, the Main() method does not return a value. Besides returning a void, the Main() method could return an integer.

Within the body of the Main() method on Line 13 is a single statement that causes the words "Howdy, Partner!" to be written to the console screen. The statement System.Console.WriteLine("Howdy, Partner!")) writes text to the console screen. Figure 1 shows the output.

Figure 1 Output from the HowdyPartner program.

  • + Share This
  • 🔖 Save To Your Account