ebook img

Learning Object-Oriented Programming in C# 5.0 PDF

671 Pages·2014·7.943 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learning Object-Oriented Programming in C# 5.0

Learning Object-Oriented Programming in C# 5.0 B. M. Harwani Cengage Learning PTR Australia•Brazil•Japan•Korea•Mexico•Singapore•Spain•UnitedKingdom•UnitedStates LearningObject-Oriented ©2015CengageLearningPTR. ProgramminginC#5.0 CENGAGEandCENGAGELEARNINGareregisteredtrademarksofCengage B.M.Harwani Learning,Inc.,withintheUnitedStatesandcertainotherjurisdictions. PublisherandGeneralManager, ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyright CengageLearningPTR:StacyL.Hiquet hereinmaybereproduced,transmitted,stored,orusedinanyformorbyany AssociateDirectorofMarketing: meansgraphic,electronic,ormechanical,includingbutnotlimitedto SarahPanella photocopying,recording,scanning,digitizing,taping,Webdistribution, informationnetworks,orinformationstorageandretrievalsystems,except ManagerofEditorialServices: aspermittedunderSection107or108ofthe1976UnitedStatesCopyright HeatherTalbot Act,withoutthepriorwrittenpermissionofthepublisher. SeniorMarketingManager: MarkHughes Forproductinformationandtechnologyassistance,contactusat ProductManager:HeatherHurley CengageLearningCustomer&SalesSupport,1-800-354-9706. Project/CopyEditor:KarenA.Gill Forpermissiontousematerialfromthistextorproduct,submit TechnicalReviewer:JoshuaSmith allrequestsonlineatcengage.com/permissions. InteriorLayoutTech:MPSLimited Furtherpermissionsquestionscanbeemailedto [email protected]. CoverDesigner:MikeTanamachi Indexer:ValerieHaynesPerry Microsoft,Windows,andInternetExplorerareeitherregisteredtrademarks Proofreader:MeganBelanger ortrademarksofMicrosoftCorporationintheUnitedStatesand/orother countries. Allothertrademarksarethepropertyoftheirrespectiveowners. Allimages©CengageLearningunlessotherwisenoted. Coverbackgroundart:©EugeneSergeev/Shutterstock.com. LibraryofCongressControlNumber:2014933750 ISBN-13:978-1-285-85456-4 ISBN-10:1-285-85456-X eISBN-10: 1-285-85457-8 CengageLearningPTR 20ChannelCenterStreet Boston,MA02210 USA CengageLearningisaleadingproviderofcustomizedlearningsolutions withofficelocationsaroundtheglobe,includingSingapore,theUnited Kingdom,Australia,Mexico,Brazil,andJapan.Locateyourlocalofficeat: international.cengage.com/region. CengageLearningproductsarerepresentedinCanadabyNelson Education,Ltd. Foryourlifelonglearningsolutions,visitcengageptr.com. Visitourcorporatewebsiteatcengage.com. PrintedintheUnitedStatesofAmerica 1234567161514 Today, the Internet is the most powerful source of information thanks to the amazing efforts of many, including the following: n Vinton Cerf, American Internet pioneer, who is recognized as the father of the Internet n Robert Kahn, American engineer who, along with Vinton Cerf, invented the Transmission Control Protocol (TCP) and the Internet Protocol (IP), the communication protocols used on the Internet n Tim Berners-Lee, British computer scientist also known as the inventor of the World Wide Web (WWW) I dedicate this book to them and to my mother, Nita Harwani. The person I am today is a product of the moral values she taught me. Acknowledgments I am grateful to Heather Hurley, product manager at Cengage Learning, for believing in me and giving me an opportunity to create this work. Thank you to Karen Gill who, as my project editor and copy editor, offered a signifi- cant amount of feedback that improved the chapters. She did first-class structural and language editing and played a vital role in improving the quality of informa- tion. I appreciate her efforts in enhancing the contents of this book and giving it a polished look. I must thank Joshua Smith, the technical editor, for his excellent, detailed reviewing of the work and the many helpful comments and suggestions he made. A great big thank-you to the editorial and production staff and the entire team at Cengage Learning who worked tirelessly to produce this book. I enjoyed working with each of you. Thanks, too, to MPS Limited for making the book look good on pages. I also want to mention my family, my small world. To my wife, Anushka, and my wonderful children, Chirag and Naman, thank you for inspiring and motivating me and above all for forgiving me for spending so much time on my computer. Speaking of encouragement, I must mention my dear students, who have been a good teacher for me. Their interesting problems and queries help me write books with a practical approach. iv About the Author B. M. Harwani is the founder and owner of Microchip Computer Education (MCE), based in Ajmer, India, which provides computer education in all programming and web developing platforms. He graduated with a BE in computer engineering from ’ the University of Pune, and he has a C Level diploma (Masters diploma in computer technology) from DOEACC, Government of India. As a teacher for more than 20 years, he has developed the art of explaining even the most complicated topics in a straightforward and easily understandable fashion. His published books include Foundation Joomla published by FriendsOfED, jQuery Recipes published by Apress, Core Data iOS Essentials published by Packt, Introduction to Python Programming and Developing GUI Applications with PyQT published by Cengage Learning, Android Programming Unleashed published by Sams Publishing, The Android Tablet ’ ’ Developers Cookbook (Developers Library) published by Addison-Wesley Profes- sional, and UNIX & Shell Programming published by Oxford University Press, USA. To learn more, visit his blog at http://bmharwani.com/blog. v Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiv Chapter 1 The .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 C#Introduction....................................................1 .NETFrameworkStructure...........................................2 CommonLanguageRuntime(CLR) ................................. 4 CommonTypeSystem ........................................... 5 CommonLanguageSpecification .................................. 5 AssemblyUse .....................................................5 VisualStudioIDEOverview ..........................................7 VisualStudioInstallation ............................................8 RunningaC#Program..............................................9 UsingtheVisualStudioIDE ...................................... 10 UsingtheCommand-LineCompiler................................ 17 Line-by-LineExplanationofCode ....................................18 ModificationoftheMain()Method..................................20 Summary........................................................22 Chapter 2 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 IdentifiersandKeywords...........................................24 Variables........................................................25 Constants .......................................................26 PredefinedDataTypes.............................................26 PredefinedValueTypes ............................................28 vi Contents vii IntegerTypes ................................................. 28 Floating-PointTypes............................................ 29 DecimalTypes................................................. 30 CharacterTypes ............................................... 31 BoolTypes.................................................... 32 NullableTypes....................................................32 PredefinedReferenceTypes.........................................34 ImplicitlyTypedVariables ..........................................37 ImplicitConversion............................................. 39 ExplicitTypeConversion......................................... 39 ReferenceTypes ..................................................40 Boxing..........................................................41 Unboxing .......................................................41 TheisOperator................................................ 42 TheasOperator ............................................... 43 Streams.........................................................43 MethodsDefinedbystreamClass ................................. 44 ThePredefinedStreams......................................... 45 ConsoleI/O ......................................................45 SendingOutputtotheConsole................................... 45 ReadingInputfromtheConsole.................................. 48 CommentUse....................................................51 WindowsFormApplication .........................................52 Summary........................................................59 Chapter 3 Operators and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 Operators .......................................................61 PrecedenceandAssociativity........................................62 AddingParentheses ............................................ 63 ThetypeofOperator ........................................... 64 ThesizeofOperator............................................ 64 CheckedandUncheckedOperators................................ 65 ArithmeticOperators..............................................67 IncrementandDecrementOperators .............................. 67 SignOperators ................................................ 68 RelationalOperators ..............................................70 EqualityOperators ................................................71 LogicalBooleanOperators..........................................72 LogicalBitwiseOperators...........................................73 ShiftOperators...................................................74 AssignmentOperators .............................................77 CompoundAssignmentOperators ................................ 77 viii Contents TernaryOperator(ConditionalOperator)..............................80 TheMathClass ...................................................81 Summary........................................................83 Chapter 4 Decision-Making and Looping. . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 ConditionalStatements ............................................85 Theif-else-ifStatement ......................................... 86 TheswitchStatement........................................... 90 TheConditionalOperator ..........................................94 Loops...........................................................96 ThewhileLoop................................................ 96 Thedo-whileLoop ............................................. 99 TheforLoop................................................. 101 ThecontinueStatement...........................................104 ThegotoStatement..............................................106 Enumeration....................................................109 Summary.......................................................111 Chapter 5 Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 UsingArrays ....................................................114 One-DimensionalArrays........................................ 114 MultidimensionalArrays ....................................... 119 UsingStrings....................................................122 PassingCommand-LineArgumentstoMain() .........................135 UsingStringBuilder...............................................135 UsingArrayList ..................................................140 Summary.......................................................148 Chapter 6 Methods and Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Methods .......................................................149 ReturnStatement............................................. 151 MethodParameters ........................................... 151 MethodInvocation............................................ 151 MethodOverloading .......................................... 153 OptionalandNamedParameters ...................................157 C#ParameterModifiers...........................................159 PassingByValue.............................................. 160 PassingByReference .......................................... 161 UsingOutputParameters....................................... 162 SendingVariableLengthParameters ............................. 164 Contents ix YourOwnType:Structures ........................................165 UsingPropertieswithStruct .................................... 168 DefiningArraysinStructures.................................... 169 UsinganArrayofStructures .................................... 172 NestedStructs...................................................174 DateandTimeStructure ..........................................179 UsingtheDateTimePropertiesandMethods....................... 179 UsingTimeSpan .............................................. 183 FormattingDatesandTimes .................................... 184 CustomDateandTimeFormatting............................... 184 StandardDateandTimeFormatting.............................. 188 Summary.......................................................191 Chapter 7 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Class...........................................................193 ThenewOperator ............................................ 195 PublicMembers .............................................. 195 DifferenceBetweenaStructandaClass........................... 197 MethodsDefinedinaClass ..................................... 197 PrivateMembers.............................................. 199 ParameterPassingbyValueandReference ........................ 201 OutputParameters............................................ 202 MethodOverloading .......................................... 204 Encapsulation...................................................205 UseofProperties ................................................206 ThethisKeyword ................................................210 Constructors ....................................................212 DefaultConstructors........................................... 213 ParameterizedConstructors..................................... 215 CopyConstructors............................................. 219 ConstructorOverloading ....................................... 221 ConstructorChaining.......................................... 224 GarbageCollection...............................................227 StaticClassesandMembers........................................228 Namespace .....................................................232 NamespaceImporting ......................................... 235 NamespaceNesting ........................................... 237 NamespaceAliasCreation ...................................... 238 InternalModifier ................................................240 Summary.......................................................241

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.