ebook img

Programming in C# A Primer PDF

526 Pages·2010·52.22 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

Third Edition E Balagurusamy Programming in C# A Primer Third 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, 4/e + Programming in Java, 4/e + Object-Oriented Programming with C++, 4/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 Third Edition E Balagurusamy Chairman EBG Foundation Coimbatore IN — Tata McGraw Hill Education Private Limited NEW DELHI McGraw-Hill Offices New Delhi New York St Louis San Francisco Auckland Bogota Caracas Kuala Lumpur Lisbon London Madrid Mexico City Milan Montreal San Juan Santiago Singapore Sydney Tokyo Toronto IN| Ea! Tata McGraw-Hill Published by Tata McGraw Hill Education Private Limited, 7 West Patel Nagar, New Delhi 110 008 Programming in C#: A Primer, 3e Copyright © 2010, 2008, 2002, by Tata McGraw Hill Education Private Limited ‘No part of this publication may be reproduced or distributed in any form or by any means, electronic, mechanical, photocopying, recording, ot otherwise or stored in a database or retrieval system without the prior written permission of the publishers. The program listings (ifany) 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, Tala McGraw Hill Education Private Limited. ISBN (13 digits): 978-0-07-070207-3, ISBN (10 digits): 0-07-070207-1 Managing Director: Ajay Shukla Head—Higher Education Publishing and Marketing: Vibha Mahajan Manager: Sponsoring—SEM & Tech Ed: Shalini Jha Asst. Sponsoring Editor: Surabhi Shukla Development Editor: Surbhi Suman Executive—Editorial Services: Sohini Mukherjee Jr Production Manager: Anjali Razdan Dy Marketing Manager—SEM & Tech Ed: Biju Ganesan General Manager—Production: Rajender P Ghansela Asst General Manager—Produetion: B L Dogra Information contained in this work has been obtained by Tata McGraw-Hill, from sources believed to be reliable. However, neither Tata McGraw-Hill nor its authors guarantee the accuracy or completeness of any information published herein, and neither Tata McGraw-Hill 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 Tata McGraw-Hill 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 Text-o-Graphics, B1/56 Arawali Apartment, Sector 34, Noida 201301 and printed at Gopsons, A~ 2 & 3, Sector ~ 64, Noida, U.P. - 201 301 Cover Printer: Gopsons DCXDRRAZRAZBD Contents Preface to the Third Edition Preface to the First Edition 1. Introducing C# 1.1 What is C#? 7 1.2. WhyC#? 7 13 Evolution of CH 3 1.4 Characteristics of C# 4 1.5 Applications of C# 5 1.6 How does C# Differ from C++ 6 1.7 How does C# Differ from Java? 7 Case Study 8 Review Questions 9 2. Understanding .NET: The C# Environment 2.1 The .Net Strategy 1 2.2 The Origins of NET Technology 12 2.3. The NET Framework 13 2.4 The Common Language Runtime 13 2.5 Framework Base Classes 15 2.6 Userand Program Interfaces 15 2.7. Visual Studio NET 15 2.8 .NET Languages /5 2.9 Benefits of the NET Approach 16 2.10 C#and the NET 16 Case Study 17 Review Questions 17 3. Overview of C# 3.1 Introduction 18 3.2. ASimple C# Program 18 3.3. Namespaces 20 3.4 Adding Comments 2/ 18 vi Contents 3.5. Main Returning a Value 22 3.6 Using Aliases for Namespace Classes 22 3.7 Passing String Objects to WriteLine Method 23 3.8 Command Line Arguments 23, 3.9 Main with a Class 25 3.10 Providing Interactive Input 26 3.11 Using Mathematical Functions 27 3.12 Multiple Main Methods 28 3.13, Compile Time Errors 28 3.14 Program Structure 29 3.15 Program Coding Style 30 Case Study 30 Common Programming Errors 31 Review Questions 32 Debugging Exercises 32 Programming Exercises 33 Literals, Variables and Data Types 4.1 Introduction 34 42 Literals 36 4.3 Variables 39 44° DataTypes 39 4.5 Value Types 40 4.6 Reference Types 43 4.7 Declaration of Variables 43 4.8 Initialization of Variables 44 49 Default Values 44 4.10 Constant Variables 45 4.11 Scope of Variables 46 4.12 Boxing and Unboxing 48 Case Study 49 Common Programming Errors 50 Review Questions 51 Debugging Exercises 52 Programming Exercises 53 5. Operators and Expressions 5.1 Introduction 55 5.2 Arithmetic Operators 55 5.3. Relational Operators 57 5.4 Logical Operators 58 5.5 Assignment Operators 59 5.6 Increment and Decrement Operators 59 5.7 Conditional Operator 60 5.8 Bitwise Operators 6/ 5.9 Special Operators 62 6. 1. 8. Contents vit 5.10 Arithmetic Expressions 62 5.11 Evaluation of Expressions 63 5.12 Precedence of Arithmetic Operators 64 5.13 Type Conversions 65 5.14 Operator Precedence and Associativity 69 5.15 Mathematical Functions 72 Case Study 73 Common Programming Errors 75 Review Questions 75 Debugging Exercises 77 Programming Exercises 77 Decision Making and Branching 80 6.1 Introduction 80 6.2 Decision Making with if Statement 80 6.3 Simple if Statement 87 64 Theif...else Statement 82 6.5 Nesting of if... else Statements 86 6.6 TheelseifLadder 88 6.7 The Switch Statement 90 6.8 The?: Operator 95 Case Study 96 Common Programming Errors 97 Review Questions 98 Debugging Exercises 99 Programming Exercises 100 Decision Making and Looping 102 7.1 Introduction 102 7.2 The while Statement 103 7.3. Thedo Statement 104 7.4 The for Statement 108 7.5 The foreach Statement 1/3 7.6 Jumps in Loops 1/4 Case Study 118 Common Programming Errors 120 Review Questions 120 Debugging Exercises 123 Programming Exercises 124 Methods in C# 125 8.1 Introduction 125 8.2 Declaring Methods 125 83 TheMainMethod 126 8.4 Invoking Methods /27 8&5 Nesting of Methods 128 vili 9% 10. Contents 8.6 Method Parameters 129 8.7 Pass by Value 129 8.8 Pass by Reference 130 8.9 The Output Parameters 131 8.10 Variable Argument Lists 132 8.11 Methods Overloading 135 Case Study 137 Common Programming Errors 140 Review Questions 141 Debugging Exercises 142 Programming Exercises 143 Handling Arrays 9.1 Introduction 145 9.2 One-Dimensional Arrays 145 9.3. Creating an Array 146 9.4 Two-Dimensional Amays 150 9.5. Variable-Size Arrays 152 9.6 The System.Array Class 153 9.7 AmrayList Class 154 Case Study 160 Common Programming Errors 163 Review Questions 163 Debugging Exercises 164 Programming Exercises 165 Manipulating Strings 10.1 Introduction 168 10.2 Creating Strings 168 10.3 String Methods 170 104 Inserting Strings /7/ 10.5 Comparing Strings /73 10.6 Finding Substrings 174 10.7 Mutable Strings 174 10.8 Arrays of Strings 177 10.9 Regular Expressions 179 Case Study 182 Common Programming Errors 184 Review Questions 185 Debugging Exercises 186 Programming Exercises 188 Structures and Enumerations 11.1 Introduction 190 11.2 Structures 190 11.3 Structs with Methods 192 145 168 190 12, 13. 11.4 Nested Structs 194 1S ifferences between Classes and Structs 11.6 Enumerations 198 11.7 Enumerator Initialization 201 11.8 Enumerator Base Types 202 11.9 Enumerator Type Conversion 203 Case Study 204 Common Programming Errors 206 Review Questions 206 Debugging Exercises 208 Programming Exercises 210 Classes and Objects 12.1 Introduction 2/2 12.2 Basic Principles of OOP 2/2 12.3. DefiningaClass 2/3 12.4 Adding Variables 2/3 12.5 Adding Methods 2/4 12.6 Member Access Modifiers 2/6 12.7. Creating Objects 277 12.8 Accessing Class Members 2/8 12.9 Constructors 2/9 12.10 Overloaded Constructors 22] 12.11 Static Members 222 12.12. Static Constructors 223 12.13 Private Constructors 223 12.14 Copy Constructors 223 12.15 Destructors 224 12.16 Member Initialization 224 12.17 The this Reference 225 12.18 Nesting of Classes 225 12.19 Constant Members 227 12.20 Read-only Members 227 12.21 Properties 228 12.22 Indexers 230 Case Study 233 Common Programming Errors 235 Review Questions 236 Debugging Exercises 240 Programming Exercises 242 Inheritance and Polymorphism 13.1 Introduction 244 13.2 Classical Inheritance 244 13.3 Containment Inheritance 245 13.4 Defining a Subclass 245 197 Contents ix 212 244

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.