You are previewing C# for Java Developers.
O'Reilly logo
C# for Java Developers

Book Description

Java developers—learn C# quickly and thoroughly with this book!

Although Java and C# share many similarities, there are fundamental differences between them. What's more, C#—the language designed from the ground up for programming the Microsoft® .NET Framework—offers a wealth of new features that enable programmers to tap the full power of .NET. This is the ideal guide to help any experts—reveal the similarities and differences between the two platforms these languages support. Then they show you how to leverage your Java experience to get up to speed in C# development with a minimum of difficulty. It's the definitive programming resource as you tackle the .NET class libraries and learn to write applications for .NET with C#.

Table of Contents

  1. C# for Java Developers
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. Introduction
      1. Why We Wrote This Book
      2. Who Should Read This Book
      3. Organization of This Book
        1. Part I: Introducing .NET
        2. Part II: The C# Language
        3. Part III: Programming .NET with C#
        4. Part IV: Advanced Topics
        5. Appendices
        6. System Requirements
        7. Support
    3. I. Introducing .NET
      1. 1. Introduction to Microsoft .NET
        1. Background
        2. Goals of .NET
          1. System Integration Using XML
          2. Platform Neutrality
          3. Language Neutrality
          4. Technology Consolidation
        3. Overview of .NET
          1. The .NET Framework
            1. Microsoft Intermediate Language
            2. The Common Language Runtime
              1. The Common Type System
              2. The Common Language Specification
            3. Base Class Libraries
          2. Development Tools
            1. Command-Line Tools
            2. Integrated Development Environment
            3. Languages
              1. Visual C# .NET
              2. Visual Basic .NET
              3. Visual C++ .NET
              4. Visual J# .NET
              5. JScript .NET
              6. Other languages
          3. Server Products
          4. Devices and Clients
          5. Building Block Services
        4. Platform Migration and Integration
          1. Web Services and Open Standards
          2. Java User Migration Path to Microsoft .NET
            1. Visual J# .NET
            2. Java Language Conversion Assistant
          3. Third-Party Products
          4. Skills Transfer
        5. Summary
      2. 2. Comparing Java and .NET Technologies
        1. Java 2 Platform, Standard Edition
        2. Java 2 Platform, Enterprise Edition
        3. Summary
    4. II. The C# Language
      1. 3. Creating Assemblies
        1. Using the C# Compiler
        2. Other .NET Framework Tools
        3. Assemblies
          1. Contents of an Assembly
            1. The Manifest
            2. Modules
            3. Resources
          2. Single-File and Multifile Assemblies
            1. Creating a Single-File Assembly
              1. Setting metadata for a single-file assembly
            2. Creating a Multifile Assembly
              1. Setting metadata for a multifile assembly
        4. Summary
      2. 4. Language Syntax and Features
        1. General Program Structure
          1. The Main Method
          2. Comments
          3. Documentation Comments
          4. Case Sensitivity
          5. Naming Conventions
          6. Source Code Character Sets and Unicode
          7. Identifiers
          8. Source File Names and Contents
        2. Keywords
          1. Package and Namespace Keywords
          2. Simple Type and Constant Keywords
          3. Type Declaration, Access, and Manipulation Keywords
          4. Modifier Keywords
          5. Flow Control Keywords
          6. Exception Handling Keywords
          7. Unmanaged Code Keywords
          8. Keywords as Identifiers
        3. Operators
          1. Precedence and Associativity
        4. Literals
        5. Modifiers
          1. Access Modifiers
          2. Inheritance Modifiers
          3. Other Modifiers
        6. Namespaces
          1. Declaring Namespaces
          2. Using Namespaces
        7. Statements
          1. Labeled Statements
          2. Selection and Iteration Statements
            1. The switch Statement
            2. The foreach Statement
          3. Jump Statements
            1. The return Statement
            2. The throw Statement
            3. The break and continue Statements
            4. The goto Statement
          4. Other Statements
            1. The try Statement
            2. The lock Statement
            3. The checked and unchecked Keywords
            4. The using Statement
            5. The fixed Statement
        8. Summary
      3. 5. Data Types
        1. Types
          1. Unified Type System
            1. Boxing
              1. Implicit boxing
            2. Unboxing
          2. Value Types
            1. Structs
              1. Declaration
              2. Inheritance
              3. Modifiers
              4. Empty constructors and structs
              5. Instantiation
              6. Members
              7. Assigning and passing structs
              8. Issues with structs
            2. Simple Types
            3. Enumerations
              1. Declaration
              2. Modifiers
              3. Members and inheritance
          3. Reference Types
            1. Classes
              1. Declaration
              2. Modifiers
              3. Members
              4. Abstract classes
              5. Default constructor accessibility
              6. Preventing class instantiation
              7. Anonymous and local classes
            2. Interfaces
              1. Declaration
              2. Modifiers
              3. Members
              4. Implementing interfaces
              5. Explicit interface implementation
            3. Arrays
              1. Declaration and creation
              2. Initialization
              3. The foreach statement
              4. Arrays as objects and collections
            4. Delegates
              1. Declaration
              2. Instantiation
              3. Invoking a delegate
              4. Modifiers
              5. Events
          4. Conversion
            1. Implicit Conversion
              1. Implicit numeric conversion
              2. Implicit enumeration conversion
              3. Implicit reference conversion
              4. Implicit boxing conversion
            2. Explicit Conversion
              1. Explicit numeric conversion
              2. Explicit enumeration conversion
              3. Explicit reference conversion
              4. Explicit unboxing conversion
            3. User-Defined Conversion
        2. Members
          1. Member Types and Declaration Context
          2. Versioning and Inheritance of Members
            1. Overriding
            2. Hiding
            3. New Virtual Members
            4. Sealed Members
            5. Base Class Member Access
          3. Static Members
          4. Member Attributes
          5. Member Modifiers
          6. Constants
            1. Declaration
            2. Modifiers
          7. Fields
            1. Declaration
            2. Modifiers
            3. Fields as Struct Members
            4. Read-Only Fields
          8. Static and Instance Constructors
            1. Declaration
            2. Modifiers
            3. Static Constructors
          9. Destructors
            1. IDisposable
          10. Methods
            1. Declaration
            2. Modifiers
            3. Parameters
              1. Parameter arrays
          11. Events
            1. Declaration
              1. Event invocation and usage
              2. .NET implementation guidelines
              3. Events and inheritance
              4. Modifiers
              5. Accessors
          12. Properties
            1. Declaration
            2. Modifiers
            3. Properties as Members of Interfaces
          13. Indexers
            1. Declaration
            2. Modifiers
            3. Indexers as Members of Interfaces
          14. Operators
            1. Declaration
              1. Unary operators
              2. Binary operators
              3. Conversion operators
            2. Cross-Language Considerations
          15. Nested Types
        3. Variables
          1. Value Parameters
          2. Reference Parameters
          3. Output Parameters
        4. Summary
      4. 6. Advanced Language Features
        1. Threading and Synchronization
        2. Exceptions and Exception Handling
          1. Declaring Exceptions
          2. Catching Exceptions
          3. Throwing Exceptions
          4. The Exception Hierarchy
          5. The System.Exception Class
            1. Exception Chaining
        3. Attributes
          1. Attribute Names
          2. Attribute Specification
            1. Attribute Parameters
              1. Positional parameters
              2. Named parameters
            2. Attribute Target Specification and Global Attributes
          3. Custom Attributes
            1. The System.AttributeUsage Attribute
            2. Attribute Declaration
            3. Attribute Constructors
            4. Named Parameters
          4. Compile-Time Attributes
            1. System.Diagnostics.ConditionalAttribute
            2. System.ObsoleteAttribute
        4. Assertions
        5. Preprocessor Directives
          1. #define and #undef
          2. #if, #elif, #else, and #endif
          3. #region and #endregion
          4. #warning and #error
          5. #line
        6. Unsafe Code
          1. The unsafe Modifier
          2. The /unsafe Compiler Flag
          3. Managed and Unmanaged Types
          4. The sizeof Operator
          5. Pointers
            1. Declaration
            2. Address-Of Operator and Pointer Indirection
            3. Pointer Member Access
            4. Pointer Element Access
            5. Pointer Arithmetic
            6. Pointer Comparison
            7. Pointer Conversion
          6. The fixed Statement
          7. The stackalloc Command
        7. Summary
    5. III. Programming .NET with C#
      1. 7. Strings and Regular Expressions
        1. Strings
          1. Creating Strings
          2. Comparing Strings
          3. Copying Strings
          4. String Length
          5. String Concatenation
          6. Changing Case
          7. Working with Characters
          8. Working with Substrings
          9. Splitting Strings
          10. Strings as Keys
          11. Parsing Strings
          12. Formatting Strings
            1. The IFormattable Interface
          13. Encoding Strings
          14. Dynamically Building Strings
        2. Regular Expressions
          1. Compiling Regular Expressions
            1. Regular Expression Compilation Flags
            2. The RegexOptions.Compiled Flag
          2. Manipulating Text
            1. Matching Regular Expressions
            2. Replacing Substrings
            3. Splitting Strings
          3. Ad Hoc Regular Expressions
        3. Summary
      2. 8. Numbers and Dates
        1. Numbers
          1. Numbers as Objects
            1. Interfaces
              1. IComparable
              2. IFormattable
              3. IConvertible
            2. Members
              1. Floating-point members
              2. Decimal members
          2. Converting Numbers
          3. Formatting Numbers
            1. Standard Numeric Format Strings
            2. Custom Numeric Format Strings
          4. Mathematical Functions
          5. Random Numbers
        2. Dates and Times
          1. System.TimeSpan
          2. Creating Dates
          3. Manipulating Dates
          4. Formatting Dates
          5. Parsing Strings
        3. Summary
      3. 9. Collections
        1. Indexers
        2. Collection Interfaces
          1. ICollection
          2. IEnumerable, IEnumerator, and the foreach Keyword
          3. IComparer and IComparable
          4. Other Collection Interfaces
        3. Basic Collections
          1. Arrays
            1. Arrays as Objects
          2. Hashtable
          3. ArrayList
          4. Queue
          5. Stack
          6. SortedList
        4. Specialized Collections
          1. Strongly Typed Collections
            1. NameObjectCollectionBase and NameValueCollection
            2. StringCollection
            3. StringDictionary
          2. Unusual Collections
            1. ListDictionary
            2. HybridDictionary
        5. The CollectionsUtil Class
        6. Synchronization
        7. Custom Collections
        8. Summary
      4. 10. Streams, Files, and I/O
        1. Working with the Console
          1. Writing to the Console
          2. Reading from the Console
          3. Changing the Console Streams
          4. Console Summary
        2. The File System
          1. Paths
          2. Files and Directories
            1. Differentiating Between Files and Directories
              1. Using the Exists methods
              2. Using bitwise operations
            2. Getting Streams from Files
              1. Creating a stream directly
              2. Creating a stream indirectly
            3. Comparison with Java
        3. Streams
          1. The Foundation of Streams
          2. Base Streams
          3. Pass-Through Streams
          4. Readers and Writers
            1. BinaryReader and BinaryWriter
            2. TextReader and TextWriter
              1. StreamReader and StreamWriter
              2. StringReader and StringWriter
          5. Synchronizing Streams
          6. Streams Summary
        4. Asynchronous I/O
          1. Asynchronous I/O Summary
        5. Isolated Storage
          1. Obtaining and Managing the Isolated Stores
          2. Reading and Writing Isolated Data
          3. Isolated Storage Summary
        6. Object Serialization
          1. Simple Serialization
          2. Selective Serialization
          3. Custom Serialization
        7. Summary
      5. 11. XML Processing
        1. XmlNameTable
        2. XmlReader
          1. XmlTextReader
            1. Opening an XML Source
            2. XmlTextReader Properties
              1. Reader state
              2. Controlling parsing behavior
            3. Working with XML Nodes
              1. Node types
              2. Node names
              3. Node values and contents
              4. Other node properties
              5. Read operations
            4. Accessing Attributes
              1. Direct attribute value access
              2. Attribute node access
            5. Closing an XmlTextReader
            6. Unimplemented Members
          2. XmlValidatingReader
            1. Creating an XmlValidatingReader
            2. Specifying a Validation Type
            3. Validation Events
            4. Cached Schemas
            5. Differences from XmlTextReader
          3. XmlNodeReader
        3. XmlWriter
          1. XmlTextWriter
            1. XmlTextWriter Creation
            2. XmlTextWriter Status
            3. Writing XML Documents
            4. Output Formatting
            5. Namespace Support
            6. Closing the XmlTextWriter
        4. Document Object Model
          1. Key Classes
          2. Document Creation
          3. Other Key Differences
        5. XPath
          1. IXPathNavigable
          2. XPathDocument
          3. XPathNavigator
            1. Node Types
            2. Node Navigation
            3. Attribute and Namespace Node Navigation
            4. Node Properties
            5. Queries
              1. Select
              2. Evaluate
              3. Matches
            6. Optimized Queries
        6. XSL Transformations
          1. Creating a Transformer
          2. Loading a Style Sheet
          3. Transforming XML Data
        7. Extensibility
        8. Summary
    6. IV. Advanced Topics
      1. 12. Reflection
        1. Dealing with Types
          1. Local Types
          2. Foreign Types
        2. Inspecting Types
          1. Inspecting Constructors and Methods
          2. Inspecting Properties
          3. Inspecting Events
          4. Inspecting Parameters
          5. Inspecting Fields
          6. Inspecting Attributes
        3. Late Binding
          1. Instantiation
          2. Manipulation
        4. Summary
      2. 13. Threading and Synchronization
        1. Threads
          1. Creating and Starting Threads
          2. Suspending and Resuming Threads
          3. Stopping Threads
          4. Setting Thread Priorities
          5. Thread States
          6. Interrupting a Thread
          7. Local Thread Data
            1. Data Slots
            2. Metadata Annotation
          8. Timers
            1. System.Threading.Timer
            2. System.Timers.Timer
          9. Basic Threading Summary
        2. The ThreadPool Class
          1. Explicit Thread Alternative
          2. Waiting for an Event
        3. Synchronization
          1. Basic Locking
          2. Waiting and Notifying
            1. Enter, TryEnter, and Exit
          3. ReaderWriterLock
          4. WaitHandle
            1. Mutex
            2. AutoResetEvent and ManualResetEvent
        4. Summary
      3. 14. Networking
        1. Accessing the Internet
          1. URLs and URIs
            1. Constructors
            2. Methods
          2. The WebClient Class
            1. Downloading Data
            2. Uploading Data
            3. Configuring WebClient
              1. Request headers
              2. Response headers
              3. Setting a base address
          3. WebRequest and WebResponse
            1. WebRequest
              1. Creating a request
              2. Configuring a request
            2. Setting Proxies
            3. Request Credentials
            4. WebResponse
              1. Getting response data
            5. Protocol-Specific Request and Response Classes
              1. File protocol classes
              2. HTTP protocol classes
          4. HTTP Connection Features
            1. Setting Defaults for All Hosts
            2. Configuring a Specific Host
          5. Names and Addresses
            1. Using the Domain Name System
            2. IP Addresses
            3. Application Endpoints
        2. Sockets
          1. Creating a TCP Client
          2. Creating a TCP Server
          3. Using UDP
        3. Programming with Native Sockets
          1. Creating Sockets
          2. Client Sockets
          3. Server Sockets
          4. Configuring Sockets
          5. Asynchronous Sockets
        4. Summary
      4. 15. Remoting
        1. First Steps
          1. Creating the Server
          2. Creating the Client
          3. Building and Testing the Example
        2. Copies and References
        3. Channels
          1. Creating a Channel
          2. Registering a Channel
          3. Using More than One Channel
        4. Publishing and Activation
          1. Client Activation
          2. Server Activation
            1. Singleton
            2. SingleCall
            3. Publishing an Existing Instance
          3. Using Configuration Files
            1. Defining Channels
            2. Server Configuration
            3. Client Configuration
          4. Publishing Limitations and Scope
            1. Methods in the Object Class
            2. Private Methods and Delegates
            3. Static Fields and Methods
        5. Lifetime Leases
          1. Configuring a Lease
          2. Renewing a Lease
            1. Renewing a Lease Directly
            2. Sponsorship Renewal
          3. Setting Lease Defaults
        6. Summary
      5. 16. Database Connectivity
        1. Data Providers
          1. Interfaces and Implementations
          2. Data Provider Sample Code
          3. Connections
            1. Creating and Opening Connections
            2. Connection Pooling
            3. Closing a Connection
            4. Changing the Active Database
          4. Transactions
          5. Commands
            1. Creating Commands
            2. Configuring Commands
              1. Configuring an IDbCommand to execute a direct SQL statement
              2. Configuring an IDbCommand to execute a stored procedure
            3. Parameters
            4. Executing Commands and Processing Results
              1. IDataReader
        2. DataSet
          1. DataSet Component Classes
          2. Creating a DataSet
          3. Managing a DataSet with a Data Adapter
          4. Manually Managing a DataSet
          5. XML Support
            1. DiffGram
            2. XmlDataDocument Synchronization
        3. Summary
      6. 17. Security and Cryptography
        1. Code Access Security
          1. Programming for CAS
          2. Declarative CAS Statements
            1. Statement Scope
            2. Declarative Statement Syntax
            3. Using Optional Permissions
            4. Refusing Permissions
            5. Assessing Declarative Statements
          3. Imperative Security Demands
          4. CAS Policies
        2. Role-Based Security
        3. Cryptography
          1. Encrypting and Decrypting Data
            1. Symmetrical Encryption
            2. Asymmetrical Encryption
          2. Hash Codes
          3. Digital Signatures
        4. Summary
      7. 18. Graphics and UI
        1. Windows Forms
          1. Using Windows Forms
            1. Adding and Disposing of Controls
            2. Starting and Stopping a Forms Application
            3. Compiling
          2. Control Overview
          3. Docking and Anchoring
            1. Anchoring
            2. Docking
          4. Handling Events
        2. Drawing with GDI+
          1. Obtaining a Graphics Instance
          2. Lines, Shapes, and Curves
          3. Drawing Strings
          4. Drawing Images
          5. Double Buffering
        3. Summary
      8. 19. Introduction to XML Web Services
        1. Creating XML Web Services
        2. Building and Deploying XML Web Services
          1. Deployment Directory
          2. Compiling
          3. Creating the Directive
          4. Configuring XML Web Services
            1. Protocols
          5. Testing XML Web Services
          6. Summary
        3. State Management
          1. Application State Management
          2. Session State Management
        4. XML Web Service Clients
          1. Creating a Proxy Class
          2. Using a Proxy Class
          3. Asynchronous Programming
        5. Summary
    7. A. Platform Integration
      1. Runtime Environment
        1. Command-Line Arguments
        2. Environment Variables
        3. Drives and Directories
        4. Machine and Operating System Information
      2. Process Control
        1. Creating New Processes
        2. Obtaining a Reference to an Existing Process
        3. Terminating Processes
        4. Information About Existing Processes
        5. Process Synchronization and Events
      3. Windows Registry
        1. The Registry Class
        2. The RegistryKey Class
        3. Remote Registry Access
      4. Windows Event Log
        1. Writing to the Event Log
          1. Registering an Event Source
          2. Writing Events
        2. Reading from the Event Log
        3. Creating and Deleting Custom Logs
        4. Event Log Notifications
      5. Windows Services
        1. The Service Application
          1. Event Logging
        2. Service Installer
          1. ServiceProcessInstaller
          2. ServiceInstaller
          3. Installer Compilation and Service Installation
        3. Service Controller
    8. B. Shared Assemblies
      1. Creating a Shared Assembly
      2. The Global Assembly Cache
        1. Assembly Versions
          1. Side-by-Side Execution
          2. Assembly Version Policies
            1. Creating a publisher policy file
            2. Creating and installing a publisher policy assembly
      3. Assembly Probing
      4. Codebase Probing
      5. Summary
    9. C. Configuring Applications
      1. Application Configuration Files
        1. Specifying a CLR Version
        2. Using Concurrent Garbage Collection
        3. Managing Assembly Versions and Locations
        4. Registering Remote Objects
      2. Application Settings
        1. Simple Settings
        2. Complex Settings
          1. Single Tag Sections
          2. Name/Value Sections
          3. Dictionary Sections
          4. Ignore Sections
      3. Summary
    10. D. Garbage Collection
      1. Controlling the Garbage Collector
        1. Forcing a Collection
        2. Generations
        3. Concurrent Collection
      2. Finalizing and Disposing
        1. Destructors
        2. Resurrection
      3. Weak References
      4. Summary
    11. E. Cross-Language Code Interoperability
      1. The Common Type System
      2. The Common Language Specification
      3. Writing CLS-Compliant Code in C#
      4. The CLSCompliant Attribute
      5. Summary
    12. F. Java to .NET API Reference
      1. The java.awt Package
      2. The java.awt.color Package
      3. The java.awt.datatransfer Package
      4. The java.awt.dnd Package
      5. The java.awt.event Package
      6. The java.awt.font Package
      7. The java.awt.geom Package
      8. The java.awt.im Package
      9. The java.awt.im.spi Package
      10. The java.awt.image Package
      11. The java.awt.image.renderable Package
      12. The java.awt.print Package
      13. The java.io Package
      14. The java.lang Package
      15. The java.lang.ref Package
      16. The java.lang.reflect Package
      17. The java.math Package
      18. The java.net Package
      19. The java.nio Package
      20. The java.rmi Package
      21. The java.security Package
      22. The java.sql Package
      23. The java.text Package
      24. The java.util Package
      25. The java.util.jar Package
      26. The java.util.logging Package
      27. The java.util.prefs Package
      28. The java.util.regex Package
      29. The java.util.zip Package
      30. The javax.swing Package
      31. The javax.swing.border Package
      32. The javax.swing.colorchooser Package
      33. The javax.swing.event Package
      34. The javax.swing.filechooser Package
      35. The javax.swing.plaf Package
      36. The javax.swing.table Package
      37. The javax.swing.text Package
      38. The javax.swing.tree Package
      39. The javax.swing.undo Package
      40. The javax.sql Package
      41. The javax.xml.parsers Package
      42. The javax.xml.transform Package
      43. The org.w3c.dom Package
      44. The org.xml.sax Package
    13. G. About the Authors
      1. Allen Jones
      2. Adam Freeman
    14. H. Pipe Valves
    15. Index
    16. About the Authors
    17. SPECIAL OFFER: Upgrade this ebook with O’Reilly