Home > Store

.NET for Java Developers: Migrating to C#

Register your product to gain access to bonus material or receive a coupon.

.NET for Java Developers: Migrating to C#

Book

  • Sorry, this book is no longer in print.
Not for Sale

Description

  • Copyright 2004
  • Dimensions: 7x9-1/4
  • Pages: 720
  • Edition: 1st
  • Book
  • ISBN-10: 0-672-32402-4
  • ISBN-13: 978-0-672-32402-4

.NET for Java Developers is the definitive guide to leveraging your Java programming experience in the .NET environment. Understanding the similarities and differences between the C# and Java APIs enables Java programmers to quickly begin rapid application Windows development using C#. The numerous code examples, which have been tested using Visual Studio® .NET 2003 and Java 1.4, show readers how to create feature-rich .NET applications by translating Java coding practices into C#.

The authors begin by detailing the .NET Framework and then immediately introduce readers to coding and the Visual Studio® .NET IDE. They compare the features of C# and Java, and provide a systematic analysis of how the two languages relate to each other. Readers cover the essentials of C# programming, from object creation to event programming, before moving into advanced areas, such as database access, XML processing, GUI programming, and reflection.

Key topics that explain how to do smart C# programming rather than emulating Java code in C#:

  • Database access
  • Inheritance and polymorphism
  • Thread programming
  • Garbage collection and memory management
  • C# application deployment
  • Delegates
  • Processing XML
  • .NET for Java Developers provides Java developers, programmers, and architects with experience-based advice for a successful transition into the .NET environment.



    0672324024B06242003

    Downloads

    Source Code

    Download the Source Code related to this title.

    Sample Content

    Online Sample Chapter

    .NET for Java Developers: Processing XML

    Downloadable Sample Chapter

    Download the Sample Chapter related to this title.

    Table of Contents



    Preface.

    I. C# ESSENTIALS.

    1. The .NET Framework.

    What Is .NET?

    NET Framework Terminology and Core Concepts.

    CLR.

    Managed Code.

    MSIL.

    Assembly.

    Application Domain.

    Runtime Host.

    The Common Type System.

    Common Language Specification.

    CLR and the JVM.

    .NET MSIL.

    .NET JIT.

    The Java Virtual Machine.

    Memory Management.

    Garbage Collection in the CLR.

    Garbage Collection in the JVM.

    Security Management.

    Policy-Driven Security Based on Code Evidence.

    Role-Based Security, Authentication, and Authorization.

    Isolated Storage.

    Cryptography.

    Security in Java.

    Exception Management.

    Exception Management in the CLR.

    Exception Management in Java.

    Overview of the .NET Framework Class Library.

    Summary.

    Chapter 2 Starting with C#.

    The .NET SDK.

    Writing Your First C# Program.

    C# Performance.

    C# Tools.

    Visual Studio .NET.

    The Intermediate Language Disassembler.

    Summary.

    3. C# and Java: What Is the Difference?

    Programming Platforms (.NET and Java).

    The Java Platform.

    The .NET Suite.

    The Presentation Layer.

    The Business Logic Layer.

    The Data Layer.

    Runtime Architecture.

    The JVM.

    The CLR.

    ASP.NET.

    Internet Explorer.

    Shell Executables.

    The OOP Model.

    Language Constructs, Syntax, and the API.

    Data Types.

    Operators.

    Control Flow Statements.

    Exception Support.

    Common Objects.

    Common Language Elements.

    Collections Library.

    Threading.

    Input/Output.

    Namespaces and Assemblies.

    What's Missing from Java?

    What's Missing from C#?

    Summary.

    4. Writing Objects.

    Object Creation.

    The C# Equivalent of java.lang.Object.

    Classes.

    Class Modifiers.

    Class Access Modifiers and Inheritance.

    Constructors.

    Methods.

    public.

    protected.

    private.

    internal.

    static.

    sealed.

    extern.

    unsafe.

    virtual.

    override.

    abstract.

    new.

    Method Inheritance.

    Method Overloading.

    Method Parameters.

    Fields.

    public.

    private.

    protected.

    internal.

    const.

    readonly.

    static. BHEADS = volatile.

    Properties.

    Inheritance and Properties.

    Variable Scoping.

    Object Destruction.

    Summary.

    5. Understanding Inheritance and Polymorphism.

    Inheritance.

    Polymorphism and Virtual Methods.

    Exploring C# Polymorphism.

    Using the virtual and override Keywords.

    Effects of Method Parameters and Conversion Rules.

    Methods and Inheritance.

    Access Modifiers.

    The base Keyword.

    Static Methods and Inheritance.

    Abstract Methods and Inheritance.

    Casting.

    Composition versus Inheritance.

    Summary.

    6. Implementing Interfaces.

    Basic Interfaces.

    Differences between C# and Java Interfaces.

    Multiple Interfaces and Explicit Interface Declaration.

    Inheritance in Interfaces.

    The as Operator.

    Summary.

    7. Data Types.

    The Java Value Type.

    The Java Reference Type.

    The C# Value Type.

    The struct Type.

    The enum Type.

    The Built-in Value Types.

    Built-in Integral Value Types.

    The Built-in Floating-Point Types.

    The Built-in Decimal Value Types.

    The Built-in Boolean Value Types.

    Explicit Conversions.

    The C# Reference Types.

    Boxing and Unboxing.

    Boxing.

    Unboxing.

    The typeof Operator.

    Unsafe Code.

    The Pointer Data Type.

    Summary.

    8. Operators.

    Arithmetic Operators.

    Logical Operators.

    Bitwise Operators.

    The & Operator.

    The | Operator.

    The ^ Operator.

    The ~ Operator.

    String Concatenation Operators.

    The Increment and Decrement Operators.

    Shift Operators.

    The << Operator.

    The >> Operator.

    Relational Operators.

    The == Operator.

    The != Operator.

    The < Operator.

    The > Operator.

    The <= Operator.

    The >= Operator.

    Assignment Operators.

    The = Operator.

    The += Operator.

    The -= Operator.

    The *= Operator.

    The /= Operator.

    The %= Operator.

    The &= Operator.

    The |= Operator.

    The ^= Operator.

    The >>= Operator.

    The <<= Operator.

    The Member Access (Dot) Operator.

    The Casting Operator.

    The Indexing Operator.

    The Conditional Operator.

    Object Creation Operator.

    Type Information Operators.

    The is Operator.

    The as Operator.

    The sizeof Operator.

    The typeof Operator.

    Overflow Exception Control.

    The checked Operator.

    The unchecked Operator.

    Pointer Type Operators.

    Operator Precedence.

    Operator Overloading.

    Summary.

    9. Essential Control Flow.

    The if Statement.

    The while Loop.

    The do while Loop.

    The for Loop.

    The switch Statement.

    The foreach Statement.

    Jump Statements.

    Summary.

    10. Programming with Exceptions.

    Fundamentals of the try-catch-finally Construct.

    Custom Exceptions.

    Inheritance and Exceptions.

    CLR Exceptions.

    Design Considerations.

    Let It Throw.

    Catch, Clean Up, and Rethrow.

    Catch, Clean Up, and Rethrow as a Different Exception.

    Summary.

    11. Working with Arrays.

    Java Versus C# Arrays.

    Java Arrays.

    C# Arrays.

    One-Dimensional Arrays.

    Multidimensional Arrays.

    Jagged Arrays.

    Copying Arrays.

    Arrays as Collections.

    Summary.

    12. Processing Strings.

    The System.String Class.

    Initializing Strings.

    The + Operator and Strings.

    Regular Expressions.

    Summary.

    13. Formatting Numbers, Strings, and Dates.

    Formatting.

    Number Formatting.

    Currency Formatting.

    Scientific Formatting.

    Number, Fixed-Point, and General Formatting.

    Percent Formatting.

    Custom Specifiers.

    Section, Hexadecimal, and Escaping Specifiers.

    Date and Time Formatting.

    Formatting Custom Objects.

    Parsing.

    Summary.

    14. Using Collections.

    The System.Collections Interfaces.

    The System.Collections Classes.

    Thread Safety.

    Type Safety.

    Immutability.

    Accessibility.

    Using the System.Collections Classes.

    ArrayList.

    BitArray.

    Hashtable.

    Queue. BHEADS = Stack.

    SortedList.

    Icomparer.

    IhashCodeProvider.

    Ienumerator.

    Type-Safe Collections.

    Custom Collections.

    Summary.

    15. Working with the C# I/O API.

    Streams.

    The FileStream Class.

    The BufferedStream Class.

    The MemoryStream Class.

    The NetworkStream Class.

    Readers and Writers.

    Files and Directories.

    AppendText.

    Exists, Copy, Move, and Delete.

    Open, OpenWrite, OpenText, and OpenRead.

    Asynchronous I/O.

    Serialization.

    Basic Serialization.

    Custom Serialization.

    Summary.

    16. Thread Programming.

    The System.Threading Namespace.

    Creating Threads Using the System.Threading.Thread Class.

    Starting a Thread.

    The Life Cycle of a Thread.

    Stopping a Thread Safely in C#.

    Creating Threads Using the System.Threading.ThreadPool Class.

    Creating Threads Using the System.Threading.Timer Class.

    Creating Threads Using the Asynchronous Callback.

    Thread Synchronization.

    The ReaderWriterLock Class.

    Interlocked Operations.

    Summary.

    17. Using C# Properties, Indexers, and Attributes.

    Properties.

    Indexers.

    Indexers with an Integer Index.

    Indexers with a String Index.

    Indexers with Multiple Indices.

    Attributes.

    Summary.

    18. Delegates and Event Programming.

    Using Delegates.

    Multicasting.

    Event Programming.

    Summary.

    II. ADVANCED C# TOPICS.

    19. Accessing Databases.

    Inside ADO.NET.

    JDBC 2.0+.

    Getting Started with ADO.NET.

    Establishing a Connection to the Database.

    Running a SELECT Query and Getting Data.

    Running an INSERT, UPDATE, or DELETE Query.

    Running a Stored Procedure.

    Processing Transactions.

    Exploring Database Metadata.

    DataSets.

    Loading Data into a DataSet.

    Saving Data to the RDBMS Using a DataSet.

    Summary.

    20. Processing XML.

    XML Support in Java.

    XML and .NET.

    Reading and Writing XML.

    Reading XML in .NET.

    Writing XML in .NET.

    Using the DOM API in .NET.

    Xpath.

    XSLT.

    Summary.

    21. GUI Programming in C#.

    Choosing a GUI Library: Java or .NET?

    Using the IDE to Develop a Small Application.

    Step 1: Creating a New Project.

    Step 2: Designing the Form.

    Step 3: Modifying the Form.

    Windows Forms, Component Controls, and Their Associated .NET Classes.

    Putting It All Together.

    What the Application Does.

    Source Files of the Application.

    The Application Controls.

    Anchoring and Docking.

    Anchoring.

    Docking.

    Drawing with .NET GUI Classes.

    System.Drawing.

    Double Buffering.

    Sample Drawing Program.

    Summary.

    22. Reflection.

    The Core Reflection Classes.

    Browsing and Querying Members.

    Invoking Methods and Setting Fields and Properties.

    Generating Dynamic IL Using Reflection Emit.

    Summary.

    23. Assemblies, Application Configuration, and Process Management.

    The .NET Assembly.

    The Assembly Manifest.

    Modules.

    Resources.

    Creating an Assembly.

    Programmatic Access to Assemblies.

    Versioning.

    Application Configuration.

    Single-Tag Section.

    Name/Value Section.

    Dictionary Section.

    A Sample Config File.

    Process Management.

    Querying the Current Application Domain.

    Executing an Application in a Remote Application Domain.

    Invoking a Method in a Remote Application Domain.

    Querying Processes.

    Creating and Killing Processes.

    Redirecting Process Output.

    Detecting Process Completion.

    Exiting a Process.

    Summary.

    Appendix: Java and C# API Comparison.

    The java.lang Package.

    The java.util Package.

    The java.io Package.

    The java.sql Package.

    The java.text Package.

    The org.w3c.dom Package.

    The javax.swing Package.

    The java.awt Package.

    Index.

    Preface

    The C# language was released by Microsoft as one of the core languages for developing .NET applications. This language 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. These differences can be very easily overlooked in your enthusiasm to transfer your Java skills to C#. All languages have their 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 and expose the differences in the two languages while underscoring good C# coding practices.

    How This Book Is Different from Other Books

    This book was written by programmers, for programmers, hence you will find the general theme of 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, Java listings have been provided. The code listings are fairly non-trivial at times and we have tried to explain concepts by walking through code. We feel this approach is similar to some of the Java open source project tutorials out there. As a Java programmer you will notice that we have taken Java quirks and idioms and tried to explore if these exist in C# too. Likewise we have also taken some features of C# and justified why they don't exist in Java and why they are designed differently in C#. We have at places given coding practices in Java which are rendered redundant in C#, for example concatenating strings with "+" for creating large string has been heavily denounced in the Java world, but is not necessarily a bad thing in C#.

    We have given each chapter its unique set of code listings instead of building up one massive application throughout all the chapters. The book focuses on discussing the C# language and not the associated tools that come with the .NET development environment.

    Is This Book for You?

    As the title suggests, this book is tailored to suit the average Java programmer. Those who program in Java on a regular basis should have a very minimal C# learning curve after reading this book. From our non-Java readers we only ask that, you are familiar with the fundamentals of object oriented programming (OOP) and have programmed with an OOP languages such i.e., 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, polymorphism and then revisit C#.

    Overview of Chapters

    You are welcome to take on any random chapter and use it as a reference for understanding the C# concept. We recommend sticking to the order of the chapters listed in this book so that you are not bogged down by syntax and construct questions when it comes to discussing the more esoteric APIs. Following is a brief description of all the chapters:

    Chapter 1 The .NET Framework

    This is an overview of the different components of the .NET framework. Readers itching to code can skip this chapter.

    Chapter 2 Starting with C#

    Here we explore the proverbial "Hello World" program first without using the Visual Studio .NET IDE (VS .NET IDE) and then using the IDE briefly explaining the different components of the IDE. The knowledge of the IDE is important if you are to be developing large scale enterprise applications, however you don't need to be well versed in the IDE to understand the language concepts.

    Chapter 3 C# and Java—What Is the Difference?

    Here we list the different features of C# and compares them to equivalent features in Java. This chapter makes for a good reading for those who want to quickly know if C# is really that different.

    Chapter 4 Writing Objects

    This chapter gets down to the business end of OOP and that is writing objects. Java programming is all about writing classes.

    Chapter 5 Understanding Inheritance and Polymorphism

    One of the cornerstones of any OOP language is polymorphism. Both Java and C# use single root class hierarchy and have polymorphism. As a Java programmer you have been probably pampered by Java's always turned on automatic virtual dispatching. The C# motto is to not have virtual dispatching turned on by default. What does that mean to you as a Java programmer? This chapter will illustrate some C# pitfalls, Java programmers might just conveniently fall into if they are not careful.

    Chapter 6 Implementing Interfaces

    Both Java and C# allow for a class to implement multiple interfaces. We will discuss several aspects of a C# interface.

    Chapter 7 Data Types

    Data types are what make a programming language. C# provides a lot more data types, constructs then Java. You should read this chapter if you want to exploit C# specific constructs and not end up writing everything as a class in C#.

    Chapter 8 Operators and Expressions

    A fairly mundane, but important topic is covered in this chapter. You may be surprised to learn that C# supports operator overloading.

    Chapter 9 Essential Control Flow

    This chapter discusses control flow statements such as if-then-else, switch, for, foreach and many more. C# is a very expressive language. There are some built-in optimizations into some of the C# constructs. You will know that C# allows strings in switch statements. There are many more surprising details discussed in this chapter.

    Chapter 10 Programming with Exceptions

    What used to be an option in C++ 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 does a programmer know what exceptions are thrown by a method he/she calls? As a Java programmer you might find C# 's lenient exception handling both a blessing and curse.

    Chapter 11 Working with Arrays

    Arrays are a very fundamental data structure found in any programming language. C# goes a step further by having a special class for it.

    Chapter 12 Processing Strings

    One of the most ubiquitous classes of Java also happens to be very commonplace in C#. This chapter discusses some of the fundamental concepts of string equality, interning, and optimizations.

    Chapter 13 Formatting Numbers, Strings and Dates

    This chapter will discuss the equivalent of the java.text.* package.

    Chapter 14 Using Collections

    Both Java and C# collections make it possible for us to build automobiles without re-inventing the wheel. Unfortunately, the C# collection library is currently no match for what Java has to offer.

    Chapter 15 Working with the C# I/O API

    We discuss IO streams, files, directories and serialization. C# makes a distinction between a file and a directory and there are several ways to make a class serializable in C#.

    Chapter 16 Threads

    Threading has been simplified a step further in C#. You can finally close threads in C#, there is a system supplied thread-pool (so no more scrambling for third party implementations) and you can have reader-writer locks on resources (something the JDK does not provide out of the box). There are four different ways to write multi-threaded classes in C#. Many more surprises unfold in this chapter.

    Chapter 17 Using C# Properties, Indexers, and Attributes

    If you were formerly a Visual Basic programmer, this chapter might bring back memories long forgotten while programming in Java. Properties reduce code clutter by eliminating getter/setter calls. Indexers let you treat collections like arrays and attributes let you tinker with the meta-data of your code. None of these are supported in Java.

    Chapter 18 Delegates

    Function pointers are abandoned in Java. They are back in C# as delegates. Another example of how C# goes out of its way to not model every concept/construct as a class.

    Chapter 19 Accessing Databases

    Accessing databases is part of the core .NET API. A very unique data structure provided in C# is the RowSet object which is basically an in-memory representation of an RDBMS table/view. This chapter discusses some of the routine database operations that you would want to execute.

    Chapter 20 Processing XML

    XML processing in Java has finally made into the core API in Java 1.4. C# provides built-in support for XML, XSL translation and XPath in its core libraries.

    Chapter 21 GUI Programming in C#

    The Java Swing library has now become the preferred API for building client side Java GUI applications. The Swing library consists of classes modeled after the MVC (Model View Controller) pattern. Although very modular, Swing can be tedious to work with because the JDK does not ship with an out of the box GUI designer. This makes it a chore to design rich interactive screens because all this has to be coded by hand. Only recently, there are third-party Java IDEs that are simplifying Swing programming.

    The .NET Windows forms library is reminiscent of Visual basic drag and drop forms with event driven code associated with the controls on the form. If you have the Visual Studio .NET handy, then GUI programming in C# is reduced to drag and drop and adding event based code. The Visual Studio .NET forms designer is a welcome change for languishing Swing programmers.

    Chapter 22 Reflection

    With managed code and type safe data types, come reflective capabilities. Reflection allows you to probe the meta-data behind the different language constructs. Both C# and Java provide a great set of reflection API. However, C#'s reflection emit capabilities takes the cake because of its ability to dynamically generate MSIL code and run it.

    Chapter 23 Assemblies, Application Configuration and Process Manangement

    Packaging is everything. This is true even for computer applications. An improperly packaged and deployed application will either not work or will give you run time headaches. C# assemblies make it possible to deploy complex applications as one unit (i.e. assembly). Assemblies can be versioned. C# also provides several deep hooks into the Windows operating system process management. Read all about assemblies and processes in this chapter.

    Code Style Used in This Book

    Code conventions vary and you might stick to the convention followed by your programming team or organization. Code is differentiated from the regular text by following a different font style (code). In this book, we avoid Hungarian notation for variable names and use the Java style for naming variables. For example:

    int count; instead of int m_IntCount;

    Method names follow the Pascal casing instead of Java's camel casing. So a Java method name

    getName() would be translated as GetName().

    loops and control flow statements follow a slanting style, where open braces appear at the end of the line and close braces appear at the beginning of the line, such as:

    for (int i = 0; i < 100; i++) {

    //Code goes here
    }

    We have commented the code in strategic places. Comments are given in places where it is not obvious as to why a particular approach was taken. We feel that the code should be as self-explanatory as possible. This can be achieved by giving meaningful names to variables and methods so that the intent of the method is conveyed without giving any additional comments. For example, it is obvious what the intent of the following method is:

    public void PrintProperties(object o)

    The code listings revolve around explaining one or two concepts. We have tried to maintain the focus of the code listing while keeping it interesting. At times where a particular concept does not deserve a full-blown class we have given code snippets.

    From the Authors to the Readers

    Writing this book has been a great learning experience for us. We were pleasantly surprised to know that C# is not all that different from Java barring a few exceptions. As a Java programmer consider yourself equipped with the basic strategies, patterns and idioms that you are used to coding in the Java world, because you can easily carry over most of these practices into the C# world. The general difference between the two languages we saw was that Java emphasizes consistency by modeling everything as a class and C# has a many more programming constructs which are not classes (i.e., enums, structs, properties, indexers, delegates, pointers). What this means to you as a Java programmer is that you could continue to model everything as a class in C# but doing so you will be robbing yourself the benefit of exploiting the built-in efficiencies in some of other C# constructs. It also makes the resulting code a little bit more expressive. With this in mind, we have encouraged you wherever possible to use the C# style because that is what they are coding in. Lastly, we encourage you to play with the Visual Studio .NET IDE. It is not necessary to use it to program in C#, but we recommend it because it will help you crank out applications and reduce some of the tedium of designing lot of big applications fast.



    0672324024P03122003

    Index

    Download the Index file related to this title.

    Updates

    Submit Errata

    More Information

    Unlimited one-month access with your purchase
    Free Safari Membership