Sie sind auf Seite 1von 5

10/2/2015

C#BasicSyntax

C#BasicSyntax
Advertisements

PreviousPage

NextPage

C# is an objectoriented programming language. In ObjectOriented Programming


methodology,aprogramconsistsofvariousobjectsthatinteractwitheachotherby
meansofactions.Theactionsthatanobjectmaytakearecalledmethods.Objectsof
thesamekindaresaidtohavethesametypeor,aresaidtobeinthesameclass.
Forexample,letusconsideraRectangleobject.Ithasattributessuchaslengthand
width.Dependinguponthedesign,itmayneedwaysforacceptingthevaluesofthese
attributes,calculatingthearea,anddisplayingdetails.
LetuslookatimplementationofaRectangleclassanddiscussC#basicsyntax:

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

A name must begin with a letter that could be followed by a sequence of


letters,digits(09)orunderscore.Thefirstcharacterinanidentifiercannot
beadigit.
Itmustnotcontainanyembeddedspaceorsymbolsuchas?+!@#%^&
*()[]{}.:"'/and\.However,anunderscore(_)canbeused.
ItshouldnotbeaC#keyword.

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

Free Answers to Your C++ Language Questions. Register Now!

Write for us

FAQ's

Helping

Contact

Copyright 2015. All Rights Reserved.


Enter email for newsletter
http://www.tutorialspoint.com/csharp/csharp_basic_syntax.htm

go
5/5

Das könnte Ihnen auch gefallen