Copyright
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers discounts on this book when ordered in quantity for bulk purchases and special sales. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside of the U.S., please contact:
International Sales
(317) 581-3793
Visit Addison-Wesley on the Web: www.awprofessional.com
Library of Congress Cataloging-in-Publication Data
Puvvala, Jawahar.
.NET for Java developers migrating to C# / Jawahar Puvvala, Alok Pota.
p. cm.
ISBN 0-672-32402-4 (alk. paper)
1. C# (Computer program language) 2. Java (Computer program language) 3. Microsoft .NET. I. Pota, Alok. II. Title.
QA76.73.C154P88 2003
005.13'3dc21
2003048068
Copyright 2004 by Pearson Education, Inc.
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada.
For information on obtaining permission for use of material from this work, please submit a written request to:
Pearson Education, Inc.
Rights and Contracts Department
75 Arlington Street, Suite 300
Boston, MA 02116
Fax: (617) 848-7047
Text printed on recycled paper
1 2 3 4 5 6 7 8 9 10CRS0706050403
First printing, July 2003
Preface
The C# language was released by Microsoft as one of the core languages for developing .NET applications. C# bears amazing similarities to Sun Microsystems' Java programming language. But along with the similarities there are a slew of differences and deviations in the overall programming methodology, syntax, and constructs. In your enthusiasm to transfer your Java skills to C#, it is easy to overlook these differences. Each language has its own good coding practices, and good Java coding practices are not necessarily good C# coding practices. The intent of this book is to bring home the fundamentals of C# from the perspective of a Java programmer. We expose the differences in the two languages while underscoring good C# coding practices.
How This Book Is Different
This book was written by programmers for programmers, and hence you will find the book to be light on description and heavy on code. You will note a preponderance of complete code samples to illustrate C# concepts, constructs, syntax, and philosophies. Where possible, we provide Java listings. The code listings are fairly nontrivial at times, and we have tried to explain concepts by walking through code. This approach is similar to some of the Java open source project tutorials out there.
As a Java programmer you will notice that we look at Java quirks and idioms to explore whether they also exist in C#. We have also examined some features of C# to discover why they don't exist in Java and why they are designed differently in C#. In places, we give coding practices in Java that are rendered redundant in C#. For example, using the + operator to concatenate strings and create large strings has been heavily denounced in the Java world, but it is not necessarily a bad thing in C#.
We have given each chapter its unique set of code listings instead of building one massive application throughout the book. Note, too, that the book focuses on discussing the C# language and not the associated tools that come with the .NET development environment.
Who Should Read This Book?
As the title suggests, this book is tailored to suit typical Java programmers. Those who regularly program in Java should have a short C# learning curve after reading this book. If you haven't programmed in Java, you should be familiar with the fundamentals of object-oriented programming (OOP) and should have programmed with an OOP language such as C++ or Smalltalk. If you are not familiar with OOP, we suggest you hone your OOP skills and get comfortable with working with classes, interfaces, objects, and polymorphism, and then revisit C#.
Overview of Chapters
Although you are welcome to consult any random chapter as a reference for understanding its C# concept, we recommend reading the chapters in order. In that way, you won't get bogged down by syntax and construct questions when it comes to discussing the more esoteric APIs. Following is a brief description of each chapter.
, The .NET Framework, is an overview of the various components of the .NET framework. Readers itching to code can skip this chapter.
, Starting with C#, explores the proverbial Hello World programfirst without using the Visual Studio .NET integrated development environment (IDE), and then using the IDE. We briefly explain the components of the IDE. Knowledge of the IDE is important if you are to develop large-scale enterprise applications. However, you don't need to be well versed in the IDE to understand the language concepts.
, C# and Java: What Is the Difference?, lists the features of C# and compares them to equivalent features in Java. This chapter makes for good reading for those who want to quickly know how C# differs from Java.
, Writing Objects, gets down to the business end of OOP: writing objects. Java programming is all about writing classes.
, Understanding Inheritance and Polymorphism, looks at the cornerstones of any OOP language. Both Java and C# use a single-root class hierarchy and have polymorphism. As a Java programmer you have probably been pampered by Java's always turned-on, automatic virtual dispatching. The C# approach is to have virtual dispatching turned off by default. What does that mean to you as a Java programmer? This chapter illustrates some C# pitfalls that Java programmers might fall into if they are not careful.
, Implementing Interfaces, explains that both Java and C# allow a class to implement multiple interfaces. We discuss several aspects of a C# interface.
, Data Types, explores the elements that make a programming language. C# provides a lot more data types and constructs than Java does. You should read this chapter if you want to exploit C#-specific constructs and not end up writing everything as a class in C#.
, Operators, covers a fairly mundane, but important, topic. You may be surprised to learn that C# supports operator overloading.
, Essential Control Flow, discusses control flow statements such as if-then-else , switch , for , foreach , and many more. C# is a very expressive language that contains built-in optimizations of some constructs. You will learn that C# allows strings in switch statements, along with many other surprising details.
, Programming with Exceptions, treats something that used to be an option in C++ and is now a major part of the Java language. Fortunately, C# and Java both agree on making exception handling a core part of the language. Unfortunately, there is no throws clause in C#. So how do you know which exceptions are thrown by a method you call? As a Java programmer you might find C#'s lenient exception handling both a blessing and a curse.