Beruflich Dokumente
Kultur Dokumente
C#BasicSyntax
C#BasicSyntax
Advertisements
PreviousPage
NextPage
http://www.tutorialspoint.com/csharp/csharp_basic_syntax.htm
1/5
10/2/2015
C#BasicSyntax
usingSystem;
namespaceRectangleApplication
{
classRectangle
{
//membervariables
doublelength;
doublewidth;
publicvoidAcceptdetails()
{
length=4.5;
width=3.5;
}
publicdoubleGetArea()
{
returnlength*width;
}
publicvoidDisplay()
{
Console.WriteLine("Length:{0}",length);
Console.WriteLine("Width:{0}",width);
Console.WriteLine("Area:{0}",GetArea());
}
}
classExecuteRectangle
{
staticvoidMain(string[]args)
{
Rectangler=newRectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
Whentheabovecodeiscompiledandexecuted,itproducesthefollowingresult:
Length:4.5
Width:3.5
Area:15.75
TheusingKeyword
ThefirststatementinanyC#programis
usingSystem;
Theusingkeywordisusedforincludingthenamespacesintheprogram.Aprogram
http://www.tutorialspoint.com/csharp/csharp_basic_syntax.htm
2/5
10/2/2015
C#BasicSyntax
canincludemultipleusingstatements.
TheclassKeyword
Theclasskeywordisusedfordeclaringaclass.
CommentsinC#
Commentsareusedforexplainingcode.Compilersignorethecommententries.The
multilinecommentsinC#programsstartwith/*andterminateswiththecharacters
*/asshownbelow:
/*Thisprogramdemonstrates
ThebasicsyntaxofC#programming
Language*/
Singlelinecommentsareindicatedbythe'//'symbol.Forexample,
}//endclassRectangle
MemberVariables
Variables are attributes or data members of a class, used for storing data. In the
precedingprogram,theRectangleclasshastwomembervariablesnamedlengthand
width.
MemberFunctions
Functionsaresetofstatementsthatperformaspecifictask.Thememberfunctionsof
a class are declared within the class. Our sample class Rectangle contains three
memberfunctions:AcceptDetails,GetAreaandDisplay.
InstantiatingaClass
In the preceding program, the class ExecuteRectangle contains the Main() method
andinstantiatestheRectangleclass.
Identifiers
Anidentifierisanameusedtoidentifyaclass,variable,function,oranyotheruser
defineditem.ThebasicrulesfornamingclassesinC#areasfollows:
http://www.tutorialspoint.com/csharp/csharp_basic_syntax.htm
3/5
10/2/2015
C#BasicSyntax
C#Keywords
KeywordsarereservedwordspredefinedtotheC#compiler.Thesekeywordscannot
beusedasidentifiers.However,ifyouwanttousethesekeywordsasidentifiers,you
mayprefixthekeywordwiththe@character.
InC#,someidentifiershavespecialmeaningincontextofcode,suchasgetandset
arecalledcontextualkeywords.
ThefollowingtableliststhereservedkeywordsandcontextualkeywordsinC#:
ReservedKeywords
abstract
as
base
bool
break
byte
case
catch
char
checked
class
const
continue
decimal
default
delegate
do
double
else
enum
event
explicit
extern
false
finally
fixed
float
for
foreach
goto
if
implicit
in
in(generic
modifier)
int
interface
internal
is
lock
long
namespace
new
null
object
operator
out
out(generic
modifier)
override
params
private
protected
public
readonly
ref
return
sbyte
sealed
short
sizeof
stackalloc
static
string
struct
switch
this
throw
true
try
typeof
uint
ulong
unchecked
unsafe
ushort
using
virtual
void
http://www.tutorialspoint.com/csharp/csharp_basic_syntax.htm
4/5
10/2/2015
volatile
C#BasicSyntax
while
ContextualKeywords
add
alias
ascending
descending
dynamic
global
group
into
join
let
partial
(method)
remove
select
set
from
orderby
PreviousPage
get
partial
(type)
NextPage
Advertisements
C++ LANGUAGE
daniweb.com
Write for us
FAQ's
Helping
Contact
go
5/5