C# Introduction Notes
C# Introduction Notes
NET History C/Win32 Programmer * Developing s/ws for windows OS involved using C pgmng language in conjunction with windows API * !sing C pgms " emplo#ing raw windows API is a comple$ jo% of pgmng * C pgms have to do manual memor# management& comple$ pointer arithmetic& C has comple$ s#ntactical constructs * 'ore over C is structured language and lac(s the advantages of OOP C++/MFC pgmr * One vast improvement over C/)in*+ pgmng is the use of C,, pgmng C,, can %e thought of as an O%ject oriented la#er on top of C * Although C,, supports * main pillars of OOP -.ncapsulation& Pol#morphism " Inheritance/ the# also have the comple$ nature of C such as manual memor# management& comple$ pointer arithmetic etc & * Despite its comple$it#& man# C,, frame wor(s e$ist such as '0C-'icrosoft 0oundation Classes/ '0C is a group/collection of )in*+ APIs& which facilitate the construction of windows applications V !." pgmr * 12 is popular due to its a%ilit# to %uild comple$ user interface& code li%raries& data access logic with ver# less comple$it# * 12 hides all the comple$ities from the pgmr 3he main draw%ac( of 12 is that it is not o%ject oriented language& rather is 4o%ject5aware6 language * 12 7 8 doesnt allow pgmr to esta%lish 4is a6 relationship %etween t#pes& and also it doesnt support multithreaded application to %e developed #a$a pgmr * 9ava language is completel# o%ject oriented and has its s#ntactic roots in C,, * 9avas strength is platform independence i e a pgm can %e written in one platform " e$ecuted in another * 9ava supports its pgmr with a large no of predefined 4pac(age6 that contain various class and interface definitions * Although 9ava offers little hope of language independence& as this goes against the 9avas primar# goal 4 a single pgmng language for ever# need6 * Pure 9ava is not appropriate for man# graphicall#/numericall# intensive applications C%M pgmr * CO' stands for 4Component O%ject 'odel6 * If a pgm is %uilt in accordance with rule of CO'& then a pgm is created which can %e used in an# other language * C,, pgmrs can %uild CO' classes that can %e used %# 12 * CO' allows onl# has5a (ind of relationship " does not provide an# support for is5a (ind of relationship * CO' is a dominant o%ject model& it is ver# comple$/difficult to develop the pgms using CO'
Win&o's (N) pgmr * D;A stands for 4Distri%uted i;ternet Architecture6 * Due to the popularit# of we%& 'icrosoft has %een adding more internet aware features into its famil# of OS * An# we% application can %e developed under the hood of windows D;A )indows D;A re<uires the use of numerous technologies li(e ASP =3'>& ?'>& 9ava Script& 12 Script& CO' etc & man# of these languages differ greatl# in their s#ntactic point of view .NET sol*tion 2elow are some core features provided %# ;.3 * 0ull interopera%ilit# with )in *+ code * Complete " total language integration ;.3 allows cross5language inheritance& cross5language e$ception handling " cross5language de%ugging * Common runtime engine shared %# all ;.3 aware languages 3his runtime engine contains a well5defined set of t#pes that each ;.3 language is aware of * 2ase class li%rar# 3his moulds all the comple$ities of raw API calls and provides the pgmr a consistent o%ject model * 2uilding %loc(s of ;.3 3hree %uilding %loc(s of ;.3 are termed as :/ C>@ ACommon >anguage @untimeB +/ C3S ACommon 3#pe S#stemB */ C>S ACommon >anguage SpecificationB :/ C+, * 0rom a pgmrs point of view ;.3 is a new runtime environment and a common %ase class li%rar# * 3he runtime la#er is properl# referred to as the Common >anguage @untime or C>@ Primar# goal of C>@ is to locate& load and manage ;.3 t#pe on %ehalf of the pgmr * C>@ also ta(es care of a num%er of low5level details such as automatic memor# management& language integration etc & +/ CT* C3S full# descri%es all possi%le data t#pes and pgmng constructs supported %# the runtime Also& specifies how these entities can interact with each other * C3S also details how the# are represented in ;.3 metadata format */ C+* C>S is a su%set of rules that are present in C3S * Defines a set of features that are common in different languages * C>S compliance is especiall# important when creating s/w components that will %e used %# other languages * If a pgm ma(es use of data t#pes or pgmng construct that is outside of C>S& then those pgms cannot %e used %# other pgmng languages
ase .lass li/rary * In addition to C>@& C3S " C>S ;.3 platform provides a %ase class li%rar# that is availa%le to all ;.3 pgmng languages * 3his %ase class encapsulates various primitives such as threads& file I/O& graphical pgmng& interaction with various hardware devices etc & Data access 3hreading F!I 2ase class li%rar# Securit# De%ugging ?'>/SOAP etc &
0ile I/O
C0 +ang*age * 'icrosoft has developed a new language specification for ;.3 frame wor( called as CC S#ntactic structure of this language is ver# similar to 9ava CC is a h#%rid of numerous languages CC language offers following features :/ ;o pointer re<uiredD CC pgms t#picall# does not need an# pointer usage +/ Automatic memor# managementD CC does not support a delete (e#word */ 0ormal s#ntactic constructs for enumerations& structures " class properties E/ C,, li(e a%ilit# to overload the operators for accustom t#pe * 3here are + categories of code that is e$isting :/ 'anaged code +/ !nmanaged code * 'anaged code are those that are e$ecuted under the control of C>@ 3hese managed codes are su%jected to certain constraints and provides several %enefits 2enefits& such as memor# management& a%ilit# to mi$ languages& %etter securit#& support for version5control and a clean wa# for s/w components to interact * Opposite of managed code is unmanaged code !nmanaged code does not e$ecute under C>@ All& windows pgm prior to creating of ;.3 is considered as unmanaged code * 0inall#& CC pgm is a managed code " there is no restriction for these pgms to commingle with unmanaged code .NET inaries/)ssem/ly * An assem%l# is a file that is automaticall# generated %# the compiler upon successful compilation of ever# ;.3 application * It can %e either a D#namic >in( >i%rar# or an e$ecuta%le file It is generated onl# once for an application and upon each su%se<uent compilation the assem%l# gets updated * An assem%l# contains Intermediate >anguage -I>/ or Common Intermediate >anguage -CI>/ code which is similar to 9ava %#te code * In the ;.3 language& it consists of metadata 'etadata enumerates the features of ever# 4t#pe6 inside the assem%l# or the %inar#
* In addition to metadata& assem%lies also have a special file called Manifest. It contains information a%out the current version of the assem%l# and other related information * In ;.3& there are two (inds of assem%lies& such as -ingle file 1 M*lti file * A -ingle file assem%l# contains all the re<uired information -I>& 'etadata and 'anifest/ in a single pac(age 3he majorit# of assem%lies in ;.3 are made up of single file assem%lies * M*lti file assem%lies are composed of numerous ;.3 %inaries or modules and are generated for larger applications One of the assem%lies will contain a manifest and others will have I> and 'etadata instructions * 'ain %enefit of I> is its power to integrate with all ;.3 languages 3his is %ecause all ;.3 languages produce the same I> code upon successful compilationG hence& the# can interact with each other ver# easil#
CC Source code
CC compiler
CO2O> to CO2O>
;.3
Source code
'SI> H 'icroSoft Intermediate >anguage MET)()T) * ;.3 metadata& in the 'icrosoft ;.3 framewor(& refers to certain data structures em%edded within the Common Intermediate >anguage code that descri%es the high5level structure of the code * 'etadata descri%es all classes and class mem%ers that are defined in the assem%l#& and the classes and class mem%ers that the current assem%l# will call from another assem%l# * 'etadata for a method contains the complete description of the method& including the class -and the assem%l# that contains the class/& the return t#pe and all of the method parameters * A ;.3 language compiler will generate the metadata and store this in the assem%l# containing the CI> * )hen the C>@ e$ecutes CI> it will chec( to ma(e sure that the metadata of the called method is the same as the metadata that is stored in the calling method * 3his ensures that a method can onl# %e called with e$actl# the right num%er of parameters and e$actl# the right parameter t#pes Common 2nterme&iate +ang*age * CI> is a language that is a%ove an# particular platform specific instruction set Irrespective of an# ;.3 aware language li(e CC& 12& CO2O> etc & the associated compiler produces CI> instructions Consider a CC pgm which will add " su%tract + nos class calc I pu%lic void Add-int $& int #/ I Console )rite>ine-4I8J I:J6&-$,#/&-$5#//G J J
class .$ I pu%lic static void 'ain- / I calc a K new calc- /G a Add-:&+/G J J * Once the a%ove CC pgm is compiled using CC compiler a single * .?. assem%l# will %e created which contains a manifest& CI> instruction " metadata descri%ing each class .$ " class calc Consider a 12 ;.3 pgm which will add " su%tract + nos class calc pu%lic 0unctoin Add-2#1al $ as integer& 2#1al # as integer/ Console )rite>ine-4I8J I:J6&-$,#/&-$5#// .nd 0unction .nd Class 'odule .$ su% 'ain- / Dim c as new calc c Add-:&+/G .nd su% .nd 'odule - In 12 ;.3 module is li(e a class which contains onl# static mem%ers / * Once the a%ove 12 ;.3 pgm is compiled in ;.3 framewor( using 12 ;.3 aware compiler a single * .?. assem%l# will %e created which also contains a manifest& CI> instruction " metadata descri%ing each class .$ " class calc * 3he assem%l# of 12 ;.3 pgm " assem%l# of CC pgm will %e almost same One advantage of compiling to CI> is language integration In order to achieve language integration all ;.3 aware languages produce same CI> 3herefore all languages are a%le to interact within a well defined %inar# arena Con$erting C2+ to platform3spe.ifi. instr*.tions * CI> contains onl# metadata& manifest " CI> instruction which are considered as platform independent instructions 3he CI> instructions are converted to platform5specific instructions %# an entit# called as 4just5in5time6 compiler -9I3 compiler/ or 9itter * 0urther a 9itter compiles CI> instructions into corresponding machine code " store the result in memor# in a manner suited to the target OS CT* C3S is a formal specification that descri%es how a given t#pe -class& structure& interface etc &/ must %e defined in order to %e hosted %# C>@ 3he formal definitions of all possi%le C3S t#pes are as follows CT- .lass type * .ver# ;.3 aware language supports a class which is the cornerstone of OOP * A class ma# contain an# no of mem%ers -such as constructors& operators& properties& methods " events/ " data points -fields/ * C3S allows a given class to support virtual " a%stract mem%ers that define a pol#morphic interface for derived classes * C3S classes are derived from a single %ase class L
CT- -tr*.t*re types * 3he concept of structures is also prevailing under C3S A structure can %e thought of as a light weight class t#peG which is considered as value data t#pes * C3S structures can have an# no of parameteriMed constructors %ut not default constructor * C3S structures are derived from a common %ase class 4S#stem 1alue3#pe6 3his %ase class ma(es it child class o%jects to ac<uire the memor# on the stac( rather than a heap * C3S permits structures to implement an# no of interfaces * Structures cannot function as the %ase t#pe to other classes or structures and are therefore e$plicitl# 4sealed6 CT- interfa.e types * Interfaces are nothing more than a named collection of a%stract mem%er definitions& which ma# %e implemented %# a class or structure * Interfaces are the onl# one in ;.3 which do not derive from a common %ase t#pe * )hen a class or structure implements interfaces in its own wa#& then the method defined in the class or structure can %e invo(ed using an interface reference in a pol#morphic manner CT- en*meration types * .numeration allow pgmr to group name/value pairs under a specific name .$D enum color I @.D& )=I3.& 2>!.JG * 2# default& the storage used to hold each item is a S#stem Int*+ =owever& it is possi%le to alter this storage also if needed * .numeration in C3S is derived from a common %ase class S#stem .num& which contains a no of mem%ers that allow pgmrs to e$tract& manipulate " transform the underl#ing name/value at runtime CT- &elegate types * Delegates are ;.3 e<uivalent for a C st#le function pointer Difference is that a ;.3 delegate is a class that derives from 4S#stem 'ulticastDelegate6& rather than a simple pointer to a raw memor# address * 0urther delegate provide intrinsic support for multicasting " as#nchronous method invocations C+* Different languages e$press the same pgmng constructs in different language specific terms .$D String concatenation in CC is done using , operator String concatenation in 12 is done using " operator * Consider the + different functions in 12 ;.3 " CC language whose return t#pe is void 12 ;.3 CC pu%lic Su% Add- / pu%lic void Add- / N IN J .nd Su% * 'inor s#ntactic variations are seen in the + functions %elonging to + different languages 3hese minor s#ntactic variations are inconse<uential for ;.3 runtime 2ut& the respective compilers such as 4v%c e$e6 for 12 ;.3 " 4csc e$e6 for CC are configured to emit the same CI> instruction set * >anguages can also differ with regard to their overall level of functionalit# .$D CC language allows operator to %e overloaded 12 ;.3 language will not allow overloading of operators * C>S is a set of guidelines that descri%e in detail& the minimal " complete set of features a given ;.35aware compiler must support to produce code that can %e hosted %# C>@ * At the same time the code can %e accessed in a uniform manner %# all languages that target the ;.3 platform C>S is a su%set of the full functionalit# defined %# C3S 7
* C>S is a set of rules that compiler %uilders must conform to All these rules are identified %# their num%ers such as C>S @ule 7 C+, * ;.3 runtime provides a single well5defined runtime la#er that is shared %# all languages " platforms that are ;.3 aware * ;.3 runtime is officiall# termed as C>@ C>@ is the name given to an assem%l# named 4mscoree dll6 -'icrosoft Common O%ject @untime .$ecution .ngine/ )hen as assem%l# is referenced for use mscoree dll is loaded automaticall#& which in turn loads the re<uired assem%l# into memor# * @untime engine mscoree dll is responsi%le for a no of tas(s 3his resolves the location of an assem%l# " finds the re<uired t#pe within the %inar# %# reading the contained metadata * .$ecution engine la#s out the t#pe in memor#& compiles the associated CI> into platform5specific instructions " then e$ecutes the code * In addition to loading custom assem%lies " creating e$ecuta%le code& C>@ will also interact with t#pes contained within the ;.3 %ase class li%raries * 3he %ase class li%rar# has %een %ro(en into a no of discrete assem%lies& the (e# %inar# is mscorli% dll& which contains a large no of core t#pe within it
;.3 source code from some ;.3 aware langauge Some ;.3 Compiler
;.3 .$ecution .ngine -mscoree dll/ Class loader 9itter Platform Specific Instructions
Fig4 Wor5flo' /et'een so*r.e .o&e6 a .NET .ompiler 1 .NET e7e.*tion engine
*il&ing C0 appli.ation *sing C-C.E8E * CSC stands for 4C Sharp Compiler6 which converts CC source code to I>-Intermediate >angauge/ form * CC pgms can %e e$ecuted directl# on 1S ;.3 ID. or can %e e$ecuted directl# in command prompt issuing the commands * Some output options for CC compiler are as follows 3hese options allow the programmer to convert the code under consideration to console application& or a code li%rar# or a windows forms application etc & File o*tp*t options /out /targetDe$e /targetDli%rar# /targetDmodule /targetDwine$e .$D Meaning !sed to specif# the name of the o/p file 2# default& the name of o/p file is same as the name of the input * CS file containing the source code 2uilds * e$e console application 3his is the default file o/p t#pe 2uilds a single file * dll assem%l# 2uilds a moduleD 'odules are elements of multi5file assem%lies 2uilds a wine$e application
,eferen.ing e7ternal assem/lies * )hen 4using S#stem6& line is written in a CC pgm the mscorli% dll is automaticall# referenced during the compilation process .$D Test..s using S#stemG using S#stem )indows 0ormsG class 3est I pu%lic static void 'ain- / I Console )rite>ine-43esting : + *6/G 'essage2o$ Show-4=elloN6/G J J * As seen in the program )indows 0orm class has %een included in it It is not enough just to include the namespace S#stem )indows 0orm class& %ut the CSC .?. must %e informed during compilation time a%out which assem%l# contains the referenced namespace * 3his information can %e passed to CSC using /reference flag written shortl# as /r So& the command is written as CDP CSC /r D S#stem )indows 0orms Dll 3est cs Compiling M*ltiple -o*r.e Files * CC pgms are usuall# implemented in a multiple source code file& rather than a single source code file Test..s .$D Hellomsg..s using S#stemG
using S#stemG using S#stem )indows 0ormsG class =ellomsg I pu%lic void spea(- / I 'essage2o$ Show-4=elloN6/G J J class 3est I pu%lic static void 'ain- /
* A%ove + files are compiled as shown %elow CDPCSC /r D S#stem )indows 0orms dll 3est cs =ellomsg cs * As an alternative& if a director# contains man# * CS files which are considered as a part of the solution pgm& then a command can %e given as %elow using wild character -*/ CDPCSC /r D S#stem )indows 0orms dll /out D 3est e$e * CS As shown a%ove& when wild character * is used it is a rule to specif# the o/p file name using the flag /out A%ove command includes all * CS files present in the current director# * In order to refer multiple e$ternal assem%lies the command is as shown %elow CDP CSC /r D S#stem )indows 0orms dllG S#stem Drawing dll /out D 3est e$e * CS ,esponse files * If a comple$ CC application is to %e %uilt& which contains numerous * CS files " numerous references for it then defining all these in command prompt using flags is difficult * 3o overcome this pro%lem response files are used CC response files has the e$tension as * rsp& it contains all the instructions to %e used during compilation of current %uild Comments can %e written within response files using C s#m%ol .$D Test..s
using S#stemG class 3est I pu%lic static void 'ain- /
e7e..rsp C 3his is response file for 3est cs /target D e$e /out D 3est e$e
I J Console )rite>ine-4=ello6/G J
* A%ove file e$ec rsp is conve#ed to CSC e$e as a response file suing the s#m%ol R as shown %elow CDP CSC Re$ec rsp * 3here is a default response file in CC named as csc rsp& when CC pgms are %uilt using CSC e$e& this file will %e automaticall# referenced 9enerating /*g reports CC compiler provides a flag named /%ugreport 3his flag allows to specif# a file nameG which contains various statistics regarding current %uildG including an# errors encountered during the compilation process .$D CDP CSC /%ugreport D %ugs t$t 3est cs Comman& +ine (e/*gger * ;.3 provides a command line de%ugger named 4cord%g e$e6 3his option allows programmer to run ;.3 assem%lies under de%ug mode Cord%g is associated with several flags which assists a programmer in de%ugging a CC pgms in console mode Some of the flags are as listed %elow
Comman& +ine Flag of .or&/g.e7e %Area(B delAeteB e$AitB gAoB si oAutB So pArintB
Meaning Set or displa# current %rea(points @emove one or more %rea(points .$it the de%ugger Continue de%ugging the current process until hitting ne$t %rea(point Step into the ne$t line Step out of the current function Step over the ne$t line Print all loaded varia%les -local& arguments etc &/
* 2efore de%ugging an application using cord%g e$eG first step is to generate s#m%olic de%ugging s#m%ols for the application %# specif#ing /de%ug flag of csc e$e .$ D CD P CSC Rtest rsp /de%ug A%ove command generates a new file named test.p&/. * pd% file contains associated s#m%ols for de%ugging along with the source code Once test pd% file is generated& the assem%l# -test e$e/ must %e opened using cord%g e$e and test pd% will %e automaticall# loaded .$D cDP cord%g e$e testapp e$e De%ugging starts and an# flags listed a%ove can %e used to de%ug the application :sing Vis*al -t*&io .NET 2(E * 1isual Studio ;.3 ID. allows programmer to %uild applications using an# num%er of ;.35aware languages li(e CC& 9C& 12 ;.3& '0C& A3> or traditional C5%ased )in *+ applications Creating V- .NET Pro;e.t -ol*tion * Different (inds of CC project wor(spaces can %e created in 1S ;.3 ID. )hen 4;ew Project6 %utton is clic(ed from the 0ile menu several project wor(spaces are visi%le depending on the (ind of language we choose Some& of them are listed as %elow Pro;e.t Type )indows Application Class >i%rar# )indows Control >i%rar# )e% Control >i%rar# Console Application )indows Services Meaning 3his project t#pe represents a )indows 0orms application Allows to %uild a single file assem%l# -* dll/ Allows to %uild a single file assem%l# -* dll/ that contains custom )indows 0orms Controls Allows to %uild customiMed we% controls Console %ased applications Allows to %uild ;3/+888 services 3hese are %ac(ground wor(er applications that are launched during the OS %oot process
:8
(ifferent files asso.iate& 'ith .NET appli.ation * )hen a console application has %een selectedG a new folder of the same name will %e created that contains a num%er of starter files and project su%directories that are enlisted as %elow 9enerate& 2tem Meaning P%inPDe%ug 3his folder contains de%ug version of compiled ;.3 assem%l# Po%jP* !nder Po%j there are several su%folders used %# 1S ;.3 during the compilation process * ico An * ico file used to specif# the icon for the current program Assem%l#Info cs 3his file allows to esta%lish assem%l#5level attri%utes for current project * cs Initial class file * csproj @epresents a CC project that is loaded into a given solution * sln @epresents current 1S ;.3 solution -Collection of individual projects/ -ol*tion E7plorer Win&o' * An# application is %uilt as a project in 1S ;.3 .ach project contains an# num%er of so*r.e .o&e files& e7ternal referen.es& and reso*r.es that constitute the application as a whole * A 4solution6 is a collection of one or more 4projects6 !sing solution e$plorer windows all such items can %e viewed " opened
Solution .$plorer Solution T1Sapp -Project/ , 1Sapp Properties @eferences * cs
* Solution e$plorer window also provides a class view ta%& which shows the o%ject5oriented view of the project Advantage of this is if the programmer dou%le clic(s on a given icon& control will %e placed in the definition of it
Class 1iew 5 1Sapp .$ Aclass nameB 'ethods " data will %e listed here
* )hen right5clic(ed on a given item a conte$t5sensitive pop5up menu will %e activated 3his menu allows programmer to access a num%er of tools that allows to configure the current project settings and sort items in a variet# of wa#s ::
,eferen.ing E7ternal )ssem/lies $ia V- .NET * Several assem%lies are used in CC programs 0or e$ample S#stem Console assem%l# is used to for printing and reading information to and from the output and input device Similarl# several assem%lies are present in ;.3 platform which can %e used for suita%le purposes * CC compiler will %e informed regarding which assem%l# is used %# writing the 4using6 line Similarl# ;.3 does not automaticall# load the re<uired assem%l# during compilation process * 3herefore& in addition to specif#ing the namespaces that are used in * cs file& 1S ;.3 re<uires that a reference must %e set to actual ph#sical assem%l# * Procedure to add assem%l# e$plicitl# in 1S ;.3 is to clic( on the option Add @eference in Project menu ,*nning Vs (e/*gging * 3here are + wa#s to e$ecute an application in 1S ;.3 ID. :/ @unning -De%ugU Start without De%ugging/ +/ De%ugging -De%ugU Start De%ugging/ * )hen a project is chosen to run& programmer is instructing 1S ;.3 to ignore all %rea(points& and most important& to automaticall# prompt for a (e#stro(e %efore terminating the current console window * )hen an project is chosen to de%ug& which does not have an# %rea(points set& the application terminates so <uic(l# that the output will %e not visi%le %ecause there will %e no prompt as such So& when de%ug option is chosen then Console @ead - / line must %e written as the end of the program (o.*menting yo*r -o*r.e Co&e $ia 8M+ * 3here is a facilit# in CC to convert the source code documentation into a corresponding ?'> file )hen this is intended& first step is to ma(e use of special comment s#nta$& the triple forward slash -//// After the triple slash& pgmr is free to use an# well5formed ?'> elements& including the predefined set as shown %elow Pre&efine& 8M+ (o.*mentation Element VcW VparamW VparamrefW Meaning Indicates that te$t within a description should %e mar(ed as code Descri%es a given parameter Associates a given ?'> tag with a specific parameter
:+
.$D
using S#stemG /// Vsummar#W /// 3his is a simple Car the illustrates /// wor(ing with ?'> st#le documentation /// V/summar#W pu%lic class Car I /// Vsummar#W /// Do #ou have a sunroofX /// V/summar#W private %ool hasSunroof K falseG /// Vsummar#W /// 3he ctor lets #ou set the sunroofedness /// V/summar#W /// Vparam nameKYhasSunroofYW V/paramW pu%lic Car-%ool hasSunroof/ I this hasSunroof K hasSunroofG J N
C0 Prepro.essor (ire.ti$es * CC supports the use of various s#m%ols that allow pgmr to interact with the compilation process C0 Prepro.essor (ire.ti$es Cdefine& Cundef Cif& Celif& Celse& Cendif Cline Cerror& Cwarning Cregion& Cendregion Meaning 3o define and un5define conditional compilation s#m%ols 3o conditionall# s(ip sections of source code 3o control line num%ers emitted for errors " warnings 3o issue errors " warnings for the current %uild 3o e$plicitl# mar( sections of source code
0region 0en®ion * A %loc( of code can %e written inside these directives If done& the %loc( of code ma# %e hidden from view and identified %# a friendl# te$tual mar(er friendl# te$tual mar(er C region ;ot of much Importance pu%lic void swap - / I N J C endregion * 2# using these directives length# * cs files are easil# managea%le Code part written inside these directives can %e hidden& if needed the# can %e %rought to visi%ilit# %# dou%le clic(ing on it 0 &efine < Cdefine defines a s#m%ol& s#m%ol can %e defined& %ut cannot assign a value to a s#m%ol !sing the s#m%ol as the e$pression passed to the Cif directive& the e$pression will evaluate to true * 3he Cdefine directive must appear in the file %efore an# instructions even %efore using line .$D
:*
* A s#m%ol that is defined using Cdefine does not conflict with a varia%le of the same name 3hat is& a varia%le name will not %e passed to a preprocessor directive and a s#m%ol will %e evaluated %# a preprocessor directive 3he scope of a s#m%ol created with Cdefine is within the file in which it was defined .$D C define PA@ // PA@ is a s#m%ol without an# value Con&itional Co&e Compilation * Cif is a categor# of conditional directive 3ests a s#m%ol or s#m%ols to see if the# evaluate to true If the# do evaluate to true& the compiler evaluates all the code %etween the Cif " Cendif .$D
Cdefine A using S#stemG class e$ I pu%lic static void 'ain-/ I Cif -A/ Console )rite>ine-YZesY/ G Cendif Console )rite>ine-Y;oY/G J J
OutputD Zes ;o
* Confining to the a%ove e$ample if 4Cdefine A6 line is removed from the program then output will %e ;o& since the code inside Cif " Cendif will not %e e$ecuted 0'arning 0error * 3hese directive allow pgmr to instruct the CC compile to generate warning or error on the fl# .$D Cdefine A using S#stemG class e$ I pu%lic static void 'ain-/ I Cif -A/ Console )rite>ine-YZesY/ G Cwarning A has %een defined Cendif Cif -[A/ Console )rite>ine-YZesY/G Cerror A has not %een defined Cendif J J * If Cwarning directive is used warning will %e generated %ut the application will %e e$ecuted If Cerror directive is used and preprocessed then application will not run
:E
-ystem. En$ironment Class * 3his class allows pgmr to o%tain a no of details regarding the conte$t of OS hosting ;.3 application using various static mem%ers .$D using S#stemG class e$ I pu%lic static void 'ain-/ I // OS version Console )rite>ine-YCurrent OSD I8JY& En$ironment.%-Version/G //Current wor(ing director# Console )rite>ine-YCurrent Director#D I8JY& En$ironment.C*rrent(ire.tory/G //>ogical drives present in the s#stem stringAB drives K En$ironment.9et+ogi.al(ri$es= >G for-int iK8GiVdrives >engthGi,,/ Console )rite>ine-YDrive I8J D I:JY& i&drivesAiB/G //1ersion of ;et platform Console )rite>ine-Y1ersion of ;.3 D I8JY& En$ironment.Version/G J J
:L