10/2/2015
C#BasicSyntax
C#BasicSyntax
Advertisements
PreviousPage
NextPage
C# is an objectoriented programming language. In ObjectOriented Programming
methodology,aprogramconsistsofvariousobjectsthatinteractwitheachotherby
[Link]
thesamekindaresaidtohavethesametypeor,aresaidtobeinthesameclass.
Forexample,[Link]
[Link],itmayneedwaysforacceptingthevaluesofthese
attributes,calculatingthearea,anddisplayingdetails.
LetuslookatimplementationofaRectangleclassanddiscussC#basicsyntax:
[Link]
1/5
10/2/2015
C#BasicSyntax
usingSystem;
namespaceRectangleApplication
{
classRectangle
{
//membervariables
doublelength;
doublewidth;
publicvoidAcceptdetails()
{
length=4.5;
width=3.5;
}
publicdoubleGetArea()
{
returnlength*width;
}
publicvoidDisplay()
{
[Link]("Length:{0}",length);
[Link]("Width:{0}",width);
[Link]("Area:{0}",GetArea());
}
}
classExecuteRectangle
{
staticvoidMain(string[]args)
{
Rectangler=newRectangle();
[Link]();
[Link]();
[Link]();
}
}
}
Whentheabovecodeiscompiledandexecuted,itproducesthefollowingresult:
Length:4.5
Width:3.5
Area:15.75
TheusingKeyword
ThefirststatementinanyC#programis
usingSystem;
[Link]
[Link]
2/5
10/2/2015
C#BasicSyntax
canincludemultipleusingstatements.
TheclassKeyword
Theclasskeywordisusedfordeclaringaclass.
CommentsinC#
[Link]
multilinecommentsinC#programsstartwith/*andterminateswiththecharacters
*/asshownbelow:
/*Thisprogramdemonstrates
ThebasicsyntaxofC#programming
Language*/
Singlelinecommentsareindicatedbythe'//'[Link],
}//endclassRectangle
MemberVariables
Variables are attributes or data members of a class, used for storing data. In the
precedingprogram,theRectangleclasshastwomembervariablesnamedlengthand
width.
MemberFunctions
[Link]
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
[Link]#areasfollows:
[Link]
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)[Link]
beadigit.
Itmustnotcontainanyembeddedspaceorsymbolsuchas?+!@#%^&
*()[]{}.:"'/and\.However,anunderscore(_)canbeused.
ItshouldnotbeaC#keyword.
C#Keywords
KeywordsarereservedwordspredefinedtotheC#[Link]
[Link],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
[Link]
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
[Link]
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
[Link]
go
5/5