ebook img

Programming in C#: A Primer PDF

499 Pages·2014·23.104 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 Programming in C#: A Primer

Programming in C# A Primer Fourth Edition About the Author E Balagurusamy is presently the Chairman of EBG Foundation, Coimbatore. He has been the Vice-Chancellor, Anna University, Chennai, and Member, Union Public Service Commission, New Delhi. He is a teacher, trainer, and consultant in the fields of Information Technology and Management. He holds an ME (Hons) in Electrical Engineering and PhD in Systems Engineering from the Indian Institute of Technology, Roorkee. His areas of interest include Object-Oriented Software Engineering, E-Governance, Technology Management, Business Process Re-engineering, and Total Quality Management. A prolific writer, he has authored a large number of research papers and several books. His best-selling books, among others include Fundamentals of Computers Computing Fundamentals and C Programming Programming in ANSI C, 6/e Object-Oriented Programming with C++, 6/e Programming in Java, 5/e Programming in BASIC, 3/e Numerical Methods Reliability Engineering A recipient of numerous honours and awards, he has been listed in the Directory of Who’s Who of Intellectuals and in the Directory of Distinguished Leaders in Education. Programming in C# A Primer Fourth Edition E Balagurusamy Chairman EBG Foundation Coimbatore McGraw Hill Education (India) Private Limited NEW DELHI McGraw Hill Education Offices New Delhi NewYork St Louis SanFrancisco Auckland Bogotá Caracas Kuala Lumpur Lisbon London Madrid Mexico City Milan Montreal San Juan Santiago Singapore Sydney Tokyo Toronto McGraw Hill Education (India) Private Limited Published by McGraw Hill Education (India) Private Limited P-24, Green Park Extension, New Delhi 110 016 Programming in C#: A Primer, 4e Copyright © 2015, 2010, 2008, 2002 by McGraw Hill Education (India) Private Limited. No part of this publication may be reproduced or distributed in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise or stored in a database or retrieval system without the prior written permission of the publishers. The program listings (if any) may be entered, stored and executed in a computer system, but they may not be reproduced for publication. This edition can be exported from India only by the publishers, McGraw Hill Education (India) Private Limited Print Edition ISBN-13: 978-93-5134-318-9 ISBN-10: 93-5134-318-9 eBook Edition e-ISBN-13: 978-93-5134-319-6 e-ISBN-10: 93-5134-319-7 Managing Director: Kaushik Bellani Head—Higher Education (Publishing and Marketing):Vibha Mahajan Senior Publishing Manager (SEM & Tech. Ed.):Shalini Jha Associate Sponsoring Editor:Smruti Snigdha Manager—Production Systems:Satinder S Baveja Assistant Manager—Editorial Services:Sohini Mukherjee Assistant Manager—Production:Anjali Razdan Assistant General Manager (Marketing)—Higher Education:Vijay Sarathi Product Specialist:Tina Jajoriya Senior Graphic Designer—Cover:Meenu Raghav General Manager—Production:Rajender P Ghansela Manager—Production:Reji Kumar Information contained in this work has been obtained by McGraw Hill Education (India), from sources believed to be reliable. However, neither McGraw Hill Education (India) nor its authors guarantee the accuracy or completeness of any information published herein, and neither McGraw Hill Education (India) nor its authors shall be responsible for any errors, omissions, or damages arising out of use of this information. This work is published with the understanding that McGraw Hill Education (India) and its authors are supplying information but are not attempting to render engineering or other professional services. If such services are required, the assistance of an appropriate professional should be sought. Typeset at The Composers, 260, C.A. Apt., Paschim Vihar, New Delhi 110 063 and printed at ** Cover Printer: ** ** Visit us at: www.meducation.co.in Contents Preface to the Fourth Edition xiii Preface to the First Edition xvii 1. Introducing C# 1 1.1 What is C#? 1 1.2 Why C#? 1 1.3 Evolution of C# 2 1.4 Characteristics of C# 4 1.5 Applications of C# 5 1.6 How Does C# Differ From C++? 5 1.7 How Does C# Differ From Java ? 7 Case Study 8 Review Questions 9 2. Understanding .NET: The C# Environment 10 2.1 .NET strategy 10 2.2 Origins of .NET Technology 11 2.3 .NET Framework 12 2.4 Common Language Runtime 12 2.5 Framework Base Classes 14 2.6 User and Program Interfaces 14 2.7 Visual Studio .NET 14 2.8 .NET Languages 14 2.9 Benefits of the .NET Approach 15 2.10 C# and the .NET 15 Case Study 15 Review Questions 16 3. Overview of C# 17 3.1 Introduction 17 3.2 Simple C# Program 18 3.3 Namespaces 19 3.4 Adding Comments 20 3.5 Main Returning a Value 21 3.6 Using Aliases for Namespace Classes 21 3.7 Passing String Objects to WriteLine Method 22 3.8 Command Line Arguments 22 3.9 Main with a Class 24 vi Contents 3.10 Providing Interactive Input 25 3.11 Using Mathematical Functions 25 3.12 Multiple Main Methods 26 3.13 Compile Time Errors 27 3.14 Program Structure 28 3.15 Program Coding Style 28 Case Study 28 Common Programming Errors 29 Review Questions 30 Debugging Exercises 31 Programming Exercises 31 4. Literals, Variables and Data Types 32 4.1 Introduction 32 4.2 Literals 34 4.3 Variables 37 4.4 Data Types 37 4.5 Value Types 38 4.6 Reference Types 41 4.7 Declaration of Variables 41 4.8 Initialization of Variables 41 4.9 Default Values 42 4.10 Constant Variables 42 4.11 Scope of Variables 43 4.12 Boxing and Unboxing 46 Case Study 47 Common Programming Errors 48 Review Questions 48 Debugging Exercises 49 Programming Exercises 50 5. Operators and Expressions 52 5.1 Introduction 52 5.2 Arithmetic Operators 52 5.3 Relational Operators 54 5.4 Logical Operators 55 5.5 Assignment Operators 56 5.6 Increment and Decrement Operators 56 5.7 Conditional Operator 57 5.8 Bitwise Operators 58 5.9 Special Operators 59 5.10 Arithmetic Expressions 59 5.11 Evaluation of Expressions 60 5.12 Precedence of Arithmetic Operators 61 5.13 Type Conversions 62 5.14 Operator Precedence and Associativity 66 5.15 Mathematical Functions 69 Case Study 70 Common Programming Errors 71 Review Questions 72 Debugging Exercises 73 Programming Exercises 74 Contents vii 6. Decision Making and Branching 76 6.1 Introduction 76 6.2 Decision Making with if Statement 76 6.3 Simple if Statement 77 6.4 The if... else Statement 78 6.5 Nesting of if....else Statements 82 6.6 elseºif Ladder 84 6.7 Switch Statement 86 6.8 ? : Operator 91 Case Study 92 Common Programming Errors 93 Review Questions 94 Debugging Exercises 95 Programming Exercises 96 7. Decision Making and Looping 98 7.1 Introduction 98 7.2 while Statement 99 7.3 do Statement 100 7.4 for Statement 104 7.5 foreach Statement 108 7.6 Jumps in Loops 110 Case Study 114 Common Programming Errors 115 Review Questions 116 Debugging Exercises 118 Programming Exercises 119 8. Methods in C# 121 8.1 Introduction 121 8.2 Declaring Methods 121 8.3 Main Method 123 8.4 Invoking Methods 123 8.5 Nesting of Methods 124 8.6 Method Parameters 125 8.7 Pass by Value 125 8.8 Pass by Reference 126 8.9 Output Parameters 127 8.10 Variable Argument Lists 128 8.11 Method Overloading 130 Case Study 133 Common Programming Errors 136 Review Questions 136 Debugging Exercises 138 Programming Exercises 139 9. Handling Arrays 141 9.1 Introduction 141 9.2 One-Dimensional Arrays 141 9.3 Creating an Array 142 9.4 Two-Dimensional Arrays 146 9.5 Variable-Size Arrays 148 viii Contents 9.6 System.Array Class 149 9.7 ArrayList Class 150 Case Study 156 Common Programming Errors 159 Review Questions 159 Debugging Exercises 160 Programming Exercises 161 10. Manipulating Strings 164 10.1 Introduction 164 10.2 Creating Strings 164 10.3 String Methods 166 10.4 Inserting Strings 167 10.5 Comparing Strings 169 10.6 Finding Substrings 170 10.7 Mutable Strings 170 10.8 Arrays of Strings 173 10.9 Regular Expressions 175 Case Study 178 Common Programming Errors 180 Review Questions 181 Debugging Exercises 182 Programming Exercises 184 11. Structures and Enumerations 185 11.1 Introduction 185 11.2 Structures 185 11.3 Structs with Methods 187 11.4 Nested Structs 189 11.5 Differences between Classes and Structs 192 11.6 Enumerations 192 11.7 Enumerator Initialization 196 11.8 Enumerator Base Types 197 11.9 Enumerator Type Conversion 197 Case Study 198 Common Programming Errors 201 Review Questions 201 Debugging Exercises 203 Programming Exercises 205 12. Classes and Objects 206 12.1 Introduction 206 12.2 Basic Principles of OOP 206 12.3 Defining a Class 207 12.4 Adding Variables 207 12.5 Adding Methods 208 12.6 Member Access Modifiers 210 12.7 Creating Objects 211 12.8 Accessing Class Members 211 12.9 Constructors 213 12.10 Overloaded Constructors 214 Contents ix 12.11 Static Members 215 12.12 Static Constructors 216 12.13 Private Constructors 217 12.14 Copy Constructors 217 12.15 Destructors 217 12.16 Member Initialization 217 12.17 This Reference 218 12.18 Nesting of Classes 218 12.19 Constant Members 220 12.20 Read-only Members 221 12.21 Properties 221 12.22 Indexers 223 Case Study 226 Common Programming Errors 228 Review Questions 229 Debugging Exercises 233 Programming Exercises 235 13. Inheritance and Polymorphism 237 13.1 Introduction 237 13.2 Classical Inheritance 237 13.3 Containment Inheritance 238 13.4 Defining a Subclass 238 13.5 Visibility Control 240 13.6 Defining Subclass Constructors 243 13.7 Multilevel Inheritance 245 13.8 Hierarchical Inheritance 248 13.9 Overriding Methods 248 13.10 Hiding Methods 250 13.11 Abstract Classes 251 13.12 Abstract Methods 252 13.13 Sealed Classes: Preventing Inheritance 252 13.14 Sealed Methods 252 13.15 Polymorphism 253 13.16 Extension Methods 257 Case Study 258 Common Programming Errors 260 Review Questions 261 Debugging Exercises 263 Programming Exercises 265 14. Interface: Multiple Inheritance 268 14.1 Introduction 268 14.2 Defining an Interface 269 14.3 Extending an Interface 269 14.4 Implementing Interfaces 270 14.5 Interfaces and Inheritance 273 14.6 Explicit Interface Implementation 274 14.7 Abstract Class and Interfaces 277 Case Study 280 Common Programming Errors 282 x Contents Review Questions 282 Debugging Exercises 284 15. Operator Overloading 288 15.1 Introduction 288 15.2 Overloadable Operators 288 15.3 Need for Operator Overloading 289 15.4 Defining Operator Overloading 289 15.5 Overloading Unary Operators 290 15.6 Overloading Binary Operators 291 15.7 Overloading Comparison Operators 293 Case Study 299 Common Programming Errors 301 Review Questions 302 Debugging Exercises 302 Programming Exercises 306 16. Delegates and Events 308 16.1 Introduction 308 16.2 Delegates 308 16.3 Delegate Declaration 309 16.4 Delegate Methods 309 16.5 Delegate Instantiation 310 16.6 Delegate Invocation 312 16.7 Using Delegates 312 16.8 Multicast Delegates 313 16.9 Events 315 Case Study 322 Review Questions 324 Debugging Exercises 325 17. Managing Console I/O Operations 329 17.1 Introduction 329 17.2 Console Class 329 17.3 Console Input 330 17.4 Console Output 330 17.5 Formatted Output 332 17.6 Numeric Formatting 333 17.7 Standard Numeric Format 333 17.8 Custom Numeric Format 335 Case Study 338 Review Questions 340 Debugging Exercises 341 Programming Exercises 342 18. Managing Errors and Exceptions 344 18.1 Introduction 344 18.2 What is Debugging? 344 18.3 Types of Errors 345 18.4 Exceptions 346 18.5 Syntax of Exception-Handling Code 347 18.6 Multiple Catch Statements 349

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.