codescracker


c#

C# Basic Syntax



« Previous Tutorial Next Tutorial »


To know about the basic syntax of a simple C# program, let's consider Rectangle object. It has length and width attributes. Now let's concentrate on this C# program to understand the basic C# program syntax:

/* C# Basic Syntax - Example Program */
		
using System;
namespace RectangleApp
{
	class Rectangle 
	{
		double len;
		double wid;
		public void Getdetails()
		{
			len = 4.5;    
			wid = 3.5;
		}		
		public double GetArea()
		{
			return len * wid; 
		}	
		public void PrintIt()
		{
			Console.WriteLine("Length of Rectangle = {0}", len);
			Console.WriteLine("Width of Rectangle = {0}", wid);
			Console.WriteLine("Area of Rectangle = {0}", GetArea());
		}
	}
	class ExecuteRect 
	{
		static void Main(string[] args)    // Main() function
		{
			Rectangle r = new Rectangle();
			r.Getdetails();
			r.PrintIt();
			Console.ReadLine(); 
		}	
	}
}

After compiling and executing the above C# program, it will produce the following output:

Length of Rectangle = 4.5
Width of Rectangle = 3.5
Area of Rectangle = 15.75

Comments in C#

Comments are ignored by the C# compiler. Therefore, comments are used for the explanation of the code. Comments helps in debugging the program. Comments helps in reminding the code later. In C#, comments are of two types:

Single-line Comments in C#

A single-line comments starts with //. That is, all the words in the same line after the // are referred as single-line comments. Here is an example of single-line comments in C# programming:

// this is single line comments

Multi-line Comments in C#

Multi-line comments starts with /* and ends with */. No matter, how many lines you are using. Here is an example showing the multi-line comments in C# programming:

/* hello, 
 * i am multi-line
 * comment */

Identifiers in C#

An identifier is a name simply used to identify a variable, class, function, or any other user-defined items. Here are the general and basic rules for naming an identifier:

Keywords in C#

Keywords in C#, are the reserved words, having special meaning to the C# compiler.

Note - In case, if you want to use keywords as an identifier, then you have to prefix the keyword with the @ character.

Here, the following table lists, the reserved keywords in C# programming:

abstract as base bool
break byte case default
delegate double else enum
checked event catch char
decimal explicit class const
extern false finally continue
foreach fixed float for
implicit in goto if
int null object in (generic modifier)
params operator out out (generic modifier)
internal is override interface
long namespace new lock
return private protected public
readonly sbyte sealed ref
short sizeof stackalloc static
string unsafe struct ulong
unchecked virtual void ushort
using switch this throw
true try volatile typeof
uint while do

In C# programming, some of the identifiers have special meaning in the context of code, such as get and set are called as contextual keywords. Here, the following table lists, these contextual keywords in C# programming:

global group orderby into
join add alias let
ascending descending dynamic partial (type)
from get select partial (method)
remove set

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test