< C Sharp

C Sharp/Introduction

Preamble

This article is a fast-paced introduction to C# designed for those who already have some programming experience. While you may not grasp every concept or detail right away, don't worry—each topic will be explored more thoroughly in subsequent lessons.

A Simple C# Program

Below is a basic C# console application. It prompts the user to enter their name, then responds with a greeting. To try this out, create a C# Console Application project in Visual Studio and name it "MySimpleApplication." Copy and paste the following code into the main code file, then 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("Please enter an adjective to describe yourself:");
            string adjective = Console.ReadLine();

            Console.WriteLine("{0} is {1}.", name, adjective);
            Console.ReadLine();
        }
    }
}

The "using" Directive

The using directive allows your code to reference classes in a namespace without needing to fully qualify their names. In this example, Console is a part of the System namespace, and thanks to using System;, we don't have to write System.Console every time.

The System namespace is part of the .NET Framework and contains core functionality like basic types, console input/output, math operations, and more.

Namespaces serve to organize code and avoid naming conflicts. Every .NET type resides in some namespace.

Namespace Declaration

The line namespace MySimpleApplication { defines a namespace that encapsulates all the classes within its scope. By convention, it's good practice to name your root namespace after your organization or your own name (e.g., namespace MyCompany.MyApp). This helps ensure uniqueness when your code interacts with libraries from others.

Class Definition

Inside the namespace, we define a public class Program containing a single method: Main(). This method is static, meaning it belongs to the class itself rather than an instance of it. The Main method is the entry point of every C# console application—it’s where the program begins execution.

Optionally, the Main method can accept command-line arguments and return an integer exit code:

public static int Main(string[] args) {
    // ...
    return 0;
}

Body of the Main Method

The method body contains the program's logic. Here's a breakdown of what each statement does:

  1. Console.WriteLine("Please enter your name:"); displays a prompt.
  2. string name = Console.ReadLine(); reads user input and stores it in the name variable.
  3. Another prompt and read follows for the adjective.
  4. Console.WriteLine("{0} is {1}.", name, adjective); outputs a formatted string. The {0} and {1} are format items—placeholders that are replaced by the corresponding arguments.
  5. Console.ReadLine(); at the end keeps the console window open until the user presses Enter.

The WriteLine method is similar to C++'s printf, using format strings and indexed placeholders.

Assignment

Modify the program to ask the user for:

  • Their favorite color
  • Their favorite animal

Then output a sentence like:

[name] is [adjective], loves the color [color], and dreams of being a [animal].

This helps reinforce your understanding of input/output and string formatting in C#.

See Also

Where to Go Next

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

References

This article is issued from Wikiversity. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.