Learn C# Programming Language
My First Program in C#
Printing a Line of Text on the Screen
Let’s begin by considering a simple app that displays a line of text. The app and its output illustrates several important C# language features.
1 // Text-displaying app.
2 using System;
3
4 public class FirstApp
5 {
6 // Main method begins execution of C# app
7 public static void Main( string[] args )
8 {
9 Console.WriteLine( "Hello. I am a C# Programmer!" );
10 } // end Main
11 } // end class Welcome1
Output:
Hello. I am a C# Programmer!
Line 1 - Comments
Begins with //, indicating that the remainder of the line is a comment. The C# compiler ignores comments, so they do not cause the computer to perform any action when the app is run.
A comment that begins with // is called a single-line comment, because it terminates at the end of the line on which it appears.
// single-line comment
/* This is a delimited comment.
It can be split over many lines */
Delimited comments such as can be split over several lines. This type of comment begins with the delimiter /* and ends with the delimiter */.
Line 2 - using Directive
Directive tells the compiler where to look for a class that’s used in the app. A great strength of Visual C# is its rich set of predefined classes that you can reuse rather than “reinventing the wheel.”
using System;
These classes are organized under namespaces—named collections of related classes. Collectively, .NET’s namespaces are referred to as the .NET Framework Class Library.
Line 3 - Blank Lines and Whitespace
Blank lines and space characters make code easier to read, and together with tab characters are known as whitespace. Space characters and tabs are known specifically as whitespace characters. Whitespace is ignored by the compiler.
Line 4 - Class Declaration
Every app consists of at least one class declaration that’s defined by the programmer. These are known as user-defined classes. The class keyword introduces a class declaration and is immediately followed by the class name (FirstApp).
public class FirstApp
Keywords (sometimes called reserved words) are reserved for use by C# and are always spelled with all lowercase letters.
Body of a Class Declaration
A left brace { , begins the body of every class declaration. A corresponding right brace, }, must end each class declaration.
Line 7 - Main Method
The starting point of every app. The parentheses after the identifier Main indicate that it’s an app building block called a method. Class declarations normally contain one or more methods.
public static void Main( string[] args )
For each app, one of the methods in a class must be called Main otherwise, the app will not execute. Methods are able to perform tasks and return information when they complete their tasks.
Body of a Method Declaration
The left brace begins the body of the method declaration. A corresponding right brace must end themethod’s body. Line 9 in the body of themethod is indented between the braces.
Line 9 - Printing/Displaying a Line of Text
Console.WriteLine( "Hello. I am a C# Programmer!!" );
Class Console provides standard input/output capabilities that enable apps to read and display text in the console window fromwhich the app executes. The Console.Write- Line method displays a line of text in the console window.
The string in the parentheses in is the argument to the method.
Statements
The entire line 9, including Console.WriteLine, the parentheses, the argument "Hello. I am a C# Programmer!" in the parentheses and the semicolon (;), is called a statement.
Ads Right