You are on page 1of 3

C# Type Conversion Methods

ToBoolean ToByte ToChar ToDateTime ToDecimal ToDouble ToInt16 ToInt64 ToSbyte ToSingle Converts to a singned byte type! ToString namespace TypeConversionApplication { class StringConversion { static void Main(string[] args) { int i = 75; loat = 5!"##5 ; do$%le d = &!'5"7(5&; %ool % = tr$e; Console")rite*ine(i"ToString()); Console")rite*ine( "ToString()); Console")rite*ine(d"ToString()); Console")rite*ine(%"ToString()); Console"+ead,ey(); -

Variables

sbyte, byte, short, ushort, int, uint, long, ulong, and char float and double, decimal, true and false, NULLABLE DATA TYPES C# also allows defining other value types of variable like enum and reference types of variables likeclass Syntax <datatype><variable list>; he Console class in the System namespace provides a function !ead"ine#$ for accepting input from the user and store it into a variable% Example : int num! num " #on$ert To%nt&'(#onsole )eadL%ne(**! T+E)E A)E ALS, ENU-E)AT%,N #,NSTANT AS.ELL

&perators
Arithmetic Operators (unary operator) Relational Operators (==, !=, >, <, <=, >= ) Logical Operators ( , !!, !)

"it#ise Operators (it #ill con$ert to %inary &ormat an' chec( &or true an' &alse %it %y %it) Assignment Operators )isc Operators (si*eo&, typeo&, an' +:) +: ,on'itional Expression -----i& con'ition is true + .hen $alu / : other#ise $alue 0!s ('etermine #ether the $alue o& certain type) As (,ast #ithout raising an' exception i& cast &ails) (O%1ect O%1 = ne# 2tringRea'er(34ello5)6 2tringRea'er r = O%1 as 2tring Rea'er6

'ecision (aking
%f , if else, nested if, s/itch statement, nested s/itch statement

switch statement allows a variable to be tested for e)uality against a list of values% *ach value is called a case+ and the variable being switched on is checked for each switch case% !ules,

1.

.he expression use' in a switch statement must ha$e an integral or enumerate' type, or %e o& a class type in #hich the class has a single con$ersion &unction to an integral or enumerate' type70ou can ha$e any num%er o& case statements #ithin a s#itch- Each case is &ollo#e' %y the $alue to %e compare' to an' a colon 8.he constant-expression &or a case must %e the same 'ata type as the $aria%le in the s#itch, an' it must %e a constant or a literal9:hen the $aria%le %eing s#itche' on is e;ual to a case, the statements &ollo#ing that case #ill execute until a break statement is reache' <:hen a break statement is reache', the s#itch terminates, an' the &lo# o& control 1umps to the next line &ollo#ing the s#itch statement=>ot e$ery case nee's to contain a break- !& no break appears, the &lo# o& control #ill fall throughto su%se;uent cases until a %rea( is reache'?A switch statement can ha$e an optional default case, #hich must appear at the en' o& the s#itch- .he 'e&ault case can %e use' &or per&orming a tas( #hen none o& the cases is true- >obreak is nee'e' in the 'e&ault case"REA@ 2.A.E)E>. : .erminates the loop or switch statement an' trans&ers execution to the statement imme'iately &ollo#ing the loop or s#itchC&. /.0* S 1 *(*. , ,auses the loop to s(ip the remain'er o& its %o'y an' imme'iately retest its

brea" statement
.he break statement in ,A has &ollo#ing t#o usage: B- :hen the break statement is encountere' insi'e a loop, the loop is imme'iately terminate' an' program control resumes at the next statement &ollo#ing the loop7- !t can %e use' to terminate a case in the switch statement!& you are using neste' loops ( ie- one loop insi'e another loop), the %rea( statement #ill stop the execution o& the innermost loop an' start executing the next line o& co'e a&ter the %loc(-

Synta#$
.he syntax &or a break statement in ,A is as &ollo#s %rea(6 1nother program , $sing System; namespace *oops { class .rogram { static void Main(string[] args) { /0 local varia%le de inition 0/ int a = 1#; /0 do loop e2ec$tion 0/ do { i (a == 15) { /0 s3ip t4e iteration 0/ a = a 5 1; contin$e; Console")rite*ine(6val$e o a7 {#-68 a); a55; - 94ile (a : &#); Console"+ead*ine();

*nums

You might also like