Sei sulla pagina 1di 14

C# NOTES

abstract
catch
default
explicit

as
char
delegate
extern

ReservedKeywords
base
bool
break
checked class
const
do
double
else
false
finally
fixed

foreach

goto

if

implicit

is

lock

interface internal

in

add

long
out(generic
object
operator out
modifier)
protected public
readonly ref
short
sizeof
stackalloc static
this
throw
true
try
unchecked unsafe
ushort
using
while
ContextualKeywords
alias
ascending descending dynamic

global

group

null
private
sealed
switch
ulong
volatile

partial
remove
(method)

into

join

select

set

let

byte
continue
enum
float
in(generic
modifier)
namespace

case
decimal
event
for

override

params

return
string
typeof
virtual

sbyte
struct
uint
void

from

get
partial
(type)

orderby

int
new

VALUE TYPES

Type
bool
byte
char

Represents

Range

Default
Value
False
0
'\0'

Booleanvalue
TrueorFalse
8bitunsignedinteger
0to255
16bitUnicodecharacter U+0000toU+ffff
128bitprecisedecimal
decimal valueswith2829
(7.9x1028to7.9x1028)/100to28 0.0M
significantdigits
64bitdoubleprecision (+/)5.0x10324to(+/)1.7x
double
0.0D
floatingpointtype
10308
32bitsingleprecision
float
3.4x1038to+3.4x1038
0.0F
floatingpointtype
int
32bitsignedintegertype 2,147,483,648to2,147,483,647 0
9,223,372,036,854,775,808to
long
64bitsignedintegertype
0L
9,223,372,036,854,775,807
sbyte 8bitsignedintegertype 128to127
0
short 16bitsignedintegertype 32,768to32,767
0
32bitunsignedinteger
uint
0to4,294,967,295
0
type
64bitunsignedinteger
ulong
0to18,446,744,073,709,551,615 0
type
16bitunsignedinteger
ushort
0to65,535
0
type

C#TypeConversionMethods
C#providesthefollowingbuiltintypeconversionmethods:

Sr.No

Methods&Description
ToBoolean

ConvertsatypetoaBooleanvalue,wherepossible.
ToByte

Convertsatypetoabyte.
ToChar

ConvertsatypetoasingleUnicodecharacter,wherepossible.
ToDateTime

Convertsatype(integerorstringtype)todatetimestructures.
ToDecimal

Convertsafloatingpointorintegertypetoadecimaltype.
ToDouble

Convertsatypetoadoubletype.
ToInt16

Convertsatypetoa16bitinteger.
ToInt32

8
9

Convertsatypetoa32bitinteger.
ToInt64

Convertsatypetoa64bitinteger.
ToSbyte
10

Convertsatypetoasignedbytetype.
ToSingle

11

Convertsatypetoasmallfloatingpointnumber.
ToString

12

Convertsatypetoastring.
ToType

13

Convertsatypetoaspecifiedtype.
ToUInt16

14

Convertsatypetoanunsignedinttype.
ToUInt32

15

Convertsatypetoanunsignedlongtype.
ToUInt64

16

Type
Integral
types

Convertsatypetoanunsignedbiginteger.
Example
sbyte,byte,short,ushort,int,uint,long,ulong,andchar

Floating
floatanddouble
pointtypes
Decimal
types
Boolean
types
Nullable
types

decimal
trueorfalsevalues,asassigned
Nullabledatatypes

Escapesequence

Meaning

\\

\character

\'

'character

\"

"character

\?

?character

\a

Alertorbell

\b

Backspace

\f

Formfeed

\n

Newline

\r

Carriagereturn

\t

Horizontaltab

\v
\xhh...

Verticaltab
Hexadecimalnumberofoneormoredigits

Arithmetic Operators
Operator
+

*
/
%
++

Description
Addstwooperands
Subtractssecondoperandfromthefirst
Multipliesbothoperands
Dividesnumeratorbydenumerator
ModulusOperatorandremainderofafteraninteger
division
Incrementoperatorincreasesintegervaluebyone
Decrementoperatordecreasesintegervaluebyone

Example
A+B=30
AB=10
A*B=200
B/A=2
B%A=0
A++=11
A=9

Rational Operators
Operator
==
!=
>
<
>=

<=

Description
Example
Checksifthevaluesoftwooperandsareequalornot,if
(A==B)isnottrue.
yesthenconditionbecomestrue.
Checksifthevaluesoftwooperandsareequalornot,if
(A!=B)istrue.
valuesarenotequalthenconditionbecomestrue.
Checksifthevalueofleftoperandisgreaterthanthe
(A>B)isnottrue.
valueofrightoperand,ifyesthenconditionbecomestrue.
Checksifthevalueofleftoperandislessthanthevalue
(A<B)istrue.
ofrightoperand,ifyesthenconditionbecomestrue.
Checksifthevalueofleftoperandisgreaterthanorequal
tothevalueofrightoperand,ifyesthencondition
(A>=B)isnottrue.
becomestrue.
Checksifthevalueofleftoperandislessthanorequalto
thevalueofrightoperand,ifyesthenconditionbecomes (A<=B)istrue.
true.

Logical Operators
Operator
&&
||
!

Description
Example
CalledLogicalANDoperator.Ifboththeoperandsare
(A&&B)isfalse.
nonzerothenconditionbecomestrue.
CalledLogicalOROperator.Ifanyofthetwooperandsis
(A||B)istrue.
nonzerothenconditionbecomestrue.
CalledLogicalNOTOperator.Usetoreversesthelogical
stateofitsoperand.IfaconditionistruethenLogical
!(A&&B)istrue.
NOToperatorwillmakefalse.

Bitwise Operators
p
0
0
1
1

q
0
1
1
0

p&q
0
0
1
0

p|q
0
1
1
1

p^q
0
1
0
1

Operator
&
|
^
~

<<

>>

Description
Example
BinaryANDOperatorcopiesabittotheresultifitexists
(A&B)=12,whichis00001100
inbothoperands.
BinaryOROperatorcopiesabitifitexistsineither
(A|B)=61,whichis00111101
operand.
BinaryXOROperatorcopiesthebitifitissetinone
(A^B)=49,whichis00110001
operandbutnotboth.
(~A)=61,whichis11000011in2's
BinaryOnesComplementOperatorisunaryandhasthe
complementduetoasignedbinary
effectof'flipping'bits.
number.
BinaryLeftShiftOperator.Theleftoperandsvalueis
movedleftbythenumberofbitsspecifiedbytheright
A<<2=240,whichis11110000
operand.
BinaryRightShiftOperator.Theleftoperandsvalueis
movedrightbythenumberofbitsspecifiedbytheright A>>2=15,whichis00001111
operand.

Assignment Operators
Operator
=

+=

*=

/=

%=

Description

Example

Simpleassignmentoperator,Assigns
valuesfromrightsideoperandstoleftside C=A+BassignsvalueofA+BintoC
operand
AddANDassignmentoperator,Itadds
rightoperandtotheleftoperandandassign C+=AisequivalenttoC=C+A
theresulttoleftoperand
SubtractANDassignmentoperator,It
subtractsrightoperandfromtheleft
operandandassigntheresulttoleft
operand

C=AisequivalenttoC=CA

MultiplyANDassignmentoperator,It
multipliesrightoperandwiththeleft
C*=AisequivalenttoC=C*A
operandandassigntheresulttoleft
operand
DivideANDassignmentoperator,It
dividesleftoperandwiththerightoperand C/=AisequivalenttoC=C/A
andassigntheresulttoleftoperand
ModulusANDassignmentoperator,It
takesmodulususingtwooperandsand
C%=AisequivalenttoC=C%A
assigntheresulttoleftoperand

<<=

LeftshiftANDassignmentoperator

C<<=2issameasC=C<<2

>>=

RightshiftANDassignmentoperator

C>>=2issameasC=C>>2

&=

BitwiseANDassignmentoperator

C&=2issameasC=C&2

^=
|=

bitwiseexclusiveORandassignment
operator
bitwiseinclusiveORandassignment
operator

Miscellaneous Operators

C^=2issameasC=C^2
C|=2issameasC=C|2

Operator
Description
sizeof() Returnsthesizeofadatatype.
typeof() Returnsthetypeofaclass.

Example
sizeof(int),returns4.
typeof(StreamReader);

&

Returnstheaddressofanvariable.

&a;returnsactualaddressofthevariable.

Pointertoavariable.

*a;createspointernamed'a'toavariable.

?:

ConditionalExpression

IfConditionistrue?ThenvalueX:
OtherwisevalueY

is

Determineswhetheranobjectisofacertaintype.

If(FordisCar)//checksifFordisan
objectoftheCarclass.
Objectobj=newStringReader("Hello");

as

Castwithoutraisinganexceptionifthecastfails.

StringReaderr=objasStringReader;

Operator Precedence
Category

Operator

Associativity

Postfix

()[]>.++

Lefttoright

Unary

+!~++(type)*&sizeof

Righttoleft

Multiplicative

*/%

Lefttoright

Additive

Lefttoright

Shift

<<>>

Lefttoright

Relational

<<=>>=

Lefttoright

Equality

==!=

Lefttoright

BitwiseAND

&

Lefttoright

BitwiseXOR

Lefttoright

BitwiseOR
LogicalAND
LogicalOR

|
&&
||

Lefttoright
Lefttoright
Lefttoright

Conditional

?:

Righttoleft

Assignment

=+==*=/=%=>>=<<=&=^=|=

Righttoleft

Comma

Lefttoright

Decision Making

Statement

Description

ifstatement

Anifstatementconsistsofabooleanexpression
followedbyoneormorestatements.

if...elsestatement

Anifstatementcanbefollowedbyanoptionalelse
statement,whichexecuteswhenthebooleanexpression
isfalse.

nestedifstatements

Youcanuseoneiforelseifstatementinsideanotherif
orelseifstatement(s).

switchstatement

Aswitchstatementallowsavariabletobetestedfor
equalityagainstalistofvalues.

nestedswitchstatements

Youcanuseoneswitchstatementinsideanotherswitch
statement(s).

Loop Type
whileloop

forloop
do...whileloop
nestedloops

Control Statement
breakstatement

continuestatement

Description
It repeats a statement or a group of statements
while a given condition is true. It tests the
condition before executing the loop body.
It executes a sequence of statements multiple
times and abbreviates the code that manages
the loop variable.
It is similar to a while statement, except that it
tests the condition at the end of the loop body
You can use one or more loop inside any another
while, for or do..while loop.
Description
Terminates the loop or switch statement and
transfers execution to the statement immediately
following the loop or switch.
Causes the loop to skip the remainder of its body
and immediately retest its condition prior to
reiterating.

Passing Parameters to a Method


Mechanism
Description
This method copies the actual value of an
argument into the formal parameter of the
Valueparameters
function. In this case, changes made to the
parameter inside the function have no effect on
the argument.
This method copies the reference to the memory
Referenceparameters
location of an argument into the formal
parameter. This means that changes made to the
parameter affect the argument.
Outputparameters
This method helps in returning more than one
value.

Arrays
Concept
Multidimensionalarrays
Jaggedarrays
Passingarraystofunctions
Paramarrays
TheArrayClass

Description
C# supports multidimensional arrays. The
simplest form of the multidimensional array is
the two-dimensional array.
C# supports multidimensional arrays, which are
arrays of arrays.
You can pass to the function a pointer to an array
by specifying the array's name without an index.
This is used for passing unknown number of
parameters to a function.
Defined in System namespace, it is the base
class to all arrays, and provides various
properties and methods for working with arrays.

Methods of the String Class


TheStringclasshasnumerousmethodsthathelpyouinworkingwiththestringobjects.Thefollowing
tableprovidessomeofthemostcommonlyusedmethods:

Sr.N
o

Methods
public static int Compare(string strA, string strB)

Comparestwospecifiedstringobjectsandreturnsanintegerthatindicatestheirrelativepositionin
thesortorder.

public static int Compare(string strA, string strB, bool


ignoreCase )
Comparestwospecifiedstringobjectsandreturnsanintegerthatindicatestheirrelativepositionin
thesortorder.However,itignorescaseiftheBooleanparameteristrue.

public static string Concat(string str0, string str1)


Concatenatestwostringobjects.

public static string Concat(string str0, string str1, string


str2)

public static string Concat(string str0, string str1, string


str2, string str3)

Concatenatesthreestringobjects.

Concatenatesfourstringobjects.

public bool Contains(string value)


ReturnsavalueindicatingwhetherthespecifiedStringobjectoccurswithinthisstring.

9
10
11

public static string Copy(string str)


CreatesanewStringobjectwiththesamevalueasthespecifiedstring.

public void CopyTo(int sourceIndex, char[] destination, int


destinationIndex, int count)
CopiesaspecifiednumberofcharactersfromaspecifiedpositionoftheStringobjecttoa
specifiedpositioninanarrayofUnicodecharacters.

public bool EndsWith(string value)


Determineswhethertheendofthestringobjectmatchesthespecifiedstring.

public bool Equals(string value)


DetermineswhetherthecurrentStringobjectandthespecifiedStringobjecthavethesamevalue.

public static bool Equals(string a, string b)


DetermineswhethertwospecifiedStringobjectshavethesamevalue.

public static string Format(string format, Object arg0)


12

Replacesoneormoreformatitemsinaspecifiedstringwiththestringrepresentationofa
specifiedobject.

public int IndexOf(char value)


13

14

ReturnsthezerobasedindexofthefirstoccurrenceofthespecifiedUnicodecharacterinthe
currentstring.

public int IndexOf(string value)


Returnsthezerobasedindexofthefirstoccurrenceofthespecifiedstringinthisinstance.

public int IndexOf(char value, int startIndex)


15

ReturnsthezerobasedindexofthefirstoccurrenceofthespecifiedUnicodecharacterinthis
string,startingsearchatthespecifiedcharacterposition.

public int IndexOf(string value, int startIndex)


16

Returnsthezerobasedindexofthefirstoccurrenceofthespecifiedstringinthisinstance,starting
searchatthespecifiedcharacterposition.

public int IndexOfAny(char[] anyOf)


17

Returnsthezerobasedindexofthefirstoccurrenceinthisinstanceofanycharacterinaspecified
arrayofUnicodecharacters.

public int IndexOfAny(char[] anyOf, int startIndex)


18

Returnsthezerobasedindexofthefirstoccurrenceinthisinstanceofanycharacterinaspecified
arrayofUnicodecharacters,startingsearchatthespecifiedcharacterposition.

19

public string Insert(int startIndex, string value)


Returnsanewstringinwhichaspecifiedstringisinsertedataspecifiedindexpositioninthe

currentstringobject.

20

21

22

public static bool IsNullOrEmpty(string value)


IndicateswhetherthespecifiedstringisnulloranEmptystring.

public static string Join(string separator, params string[]


value)
Concatenatesalltheelementsofastringarray,usingthespecifiedseparatorbetweeneach
element.

public static string Join(string separator, string[] value,


int startIndex, int count)
Concatenatesthespecifiedelementsofastringarray,usingthespecifiedseparatorbetweeneach
element.

public int LastIndexOf(char value)


23

ReturnsthezerobasedindexpositionofthelastoccurrenceofthespecifiedUnicodecharacter
withinthecurrentstringobject.

public int LastIndexOf(string value)


24

Returnsthezerobasedindexpositionofthelastoccurrenceofaspecifiedstringwithinthecurrent
stringobject.

public string Remove(int startIndex)


25

Removesallthecharactersinthecurrentinstance,beginningataspecifiedpositionandcontinuing
throughthelastposition,andreturnsthestring.

public string Remove(int startIndex, int count)


26

Removesthespecifiednumberofcharactersinthecurrentstringbeginningataspecifiedposition
andreturnsthestring.

public string Replace(char oldChar, char newChar)


27

ReplacesalloccurrencesofaspecifiedUnicodecharacterinthecurrentstringobjectwiththe
specifiedUnicodecharacterandreturnsthenewstring.

public string Replace(string oldValue, string newValue)


28

Replacesalloccurrencesofaspecifiedstringinthecurrentstringobjectwiththespecifiedstring
andreturnsthenewstring.

public string[] Split(params char[] separator)


29

Returnsastringarraythatcontainsthesubstringsinthecurrentstringobject,delimitedby
elementsofaspecifiedUnicodecharacterarray.

30

public string[] Split(char[] separator, int count)


Returnsastringarraythatcontainsthesubstringsinthecurrentstringobject,delimitedby
elementsofaspecifiedUnicodecharacterarray.Theintparameterspecifiesthemaximumnumber

ofsubstringstoreturn.

31
32

public bool StartsWith(string value)


Determineswhetherthebeginningofthisstringinstancematchesthespecifiedstring.

public char[] ToCharArray()


ReturnsaUnicodecharacterarraywithallthecharactersinthecurrentstringobject.

public char[] ToCharArray(int startIndex, int length)


33

34
35
36

ReturnsaUnicodecharacterarraywithallthecharactersinthecurrentstringobject,startingfrom
thespecifiedindexanduptothespecifiedlength.

public string ToLower()


Returnsacopyofthisstringconvertedtolowercase.

public string ToUpper()


Returnsacopyofthisstringconvertedtouppercase.

public string Trim()


RemovesallleadingandtrailingwhitespacecharactersfromthecurrentStringobject.

Potrebbero piacerti anche