C# Tutorial

What is C#

C# or CSharp is an object oriented programming language from Microsoft. C# is greatly inspired by C and C++ in terms of functionality and syntax. If you have programmed in any of the aforementioned languages, you will easily find your feet in C#. This C# tutorial is for beginners as well as advanced programmers, who want to learn C#.

Consider this basic C# program to understand the language syntax.

using System;

namespace CSharpTutorials {
	class BasicSyntax {
		static void Main(string[] args) {
			Console.WriteLine("Welcome to C#");
			Console.ReadKey();
		}
	}
}

Download the above code

A typical C# program consists of the following parts.

Identifiers

In the above code, System, CSharpTutorials, BasicSyntax, Main, WriteLine and ReadKey all are identifiers. These are the names by which programmers declare their namespaces, functions, classes, member variables and so on.

Keywords

In C#, Identifiers start with an alphabet or underscore. C# is a case sensitive language therefore MyVariable and myvariable are not equal. By convention, function parameters, local variables and all the private fields should be declared in camelcase while the rest of the identifiers should be declared in Pascal case.

Keywords are reserved words in C# language. We use them for specific purposes by default. We should not use these words as  identifiers. We use the using keyword to add namespace in C# program. Here “using”, “class”, “void” and “static” are keywords.

Punctuators

We use punctuators to structurally demarcate your code. There are two punctuators in the aforementioned example which are  “{ }” and “;”. Use braces for creating a statement block by grouping multiple lines of codes while semicolon is used to terminate line of code.

Literals

Literals are lexically embedded into C# code. Literals consist of primitive data. In The sample code, the statement “Welcome to C#” is a literal string. It can be anything and do not have any logical impact on the code. Integers can also be used as literal. For instance you can write statements like “2+2” in your code. Here 2 is a literal value.

Operators

Operators are functions that act upon literals or variables and return the result of the operation they perform. We denote operators with symbols. For example we denote an addition operator by a “+”.

Comments

In C#, we use comments to explain what is happening in the code. C# never executes commented code. There are two types of comments in C#:

  • Single line comments: Entire line after //
  • Double line comments which start with /* and end with */.
Enumerations in C# >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .