Problem solve Get help with specific problems with your technologies, process and projects.

Getting started with C#

How to write a basic C# program, and differences from and similarities to C++ and Java.

Getting started with C#? Just curious about the language? This tip, excerpted from InformIT, shows how to write a basic C# program, and discusses differences from and similarities to C++ and Java.

The InformIT article is excerpted from C# Unleashed. Click here to learn more about the book or to buy it.

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. In addition, the article 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.

To read the entire article from which this tip is excerpted, click over to InformIT. You have to register there, but the registration is free.

This was last published in October 2002

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.