You are previewing Microsoft® Visual C#™ .NET Language Reference.
O'Reilly logo
Microsoft® Visual C#™ .NET Language Reference

Book Description

Here’s official documentation for the Visual C#® .NET language, including descriptions of all major features. Covers types, modifiers, statements, method parameters, namespaces, keywords, XML documentation, attributes, and more.

Table of Contents

  1. Microsoft® Visual C#™ .NET: Language Reference
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. 1. Introduction to C#
      1. Programming in C#
      2. General Structure of a C# Program
    3. 2. C# Keywords
      1. The Keywords
      2. Access Keywords
        1. base
          1. Example
          2. Output
          3. Example
          4. Output
        2. this
          1. Example
          2. Output
      3. Conversion Keywords
        1. explicit
          1. Example
          2. Output
        2. implicit
          1. Example
          2. Output
        3. operator
          1. Remarks
          2. Example
          3. Output
      4. Literal Keywords
        1. null
          1. See Also
        2. true
          1. true Operator
          2. true Literal
            1. Example
            2. Output
            3. See Also
        3. false
          1. false Operator
          2. false Literal
            1. Example
            2. Output
            3. See Also
      5. Method Parameter Keywords
        1. params
          1. Example
          2. Output
        2. ref
          1. Example
          2. Output
        3. out
          1. Example
          2. Output
      6. Modifiers
        1. Access Modifiers
          1. Accessibility Levels
            1. See Also
          2. Accessibility Domain
            1. Example
            2. See Also
          3. Restrictions on Using Accessibility Levels
            1. Example
            2. See Also
          4. internal
            1. Example
              1. File Assembly1.cs
              2. File Assembly2.cs
            2. See Also
          5. private
            1. Example
            2. See Also
          6. protected
            1. Example
            2. Output
            3. See Also
          7. public
            1. Example
            2. Output
            3. See Also
        2. abstract
          1. Example
          2. Output
          3. See Also
        3. const
          1. Remarks
          2. Example
          3. Output
          4. Example
          5. Output
        4. event
          1. Remarks
          2. Examples
          3. Example 1
          4. Example 2
          5. Example 3
          6. See Also
        5. extern
          1. Example
          2. Sample Run
          3. Example
            1. File: Cmdll.c
            2. File: CM.cs
          4. Output
          5. Compilation
        6. override
          1. Example
          2. Example
          3. Output
          4. See Also
        7. readonly
          1. Example
          2. Output
        8. sealed
          1. Example
          2. Output
        9. static
          1. Remarks
          2. Example
          3. Input
          4. Sample Output
        10. unsafe
          1. Example
          2. Output
          3. See Also
        11. virtual
          1. Example
          2. Output
          3. See Also
        12. volatile
          1. Remarks
          2. Example
      7. Namespace Keywords
        1. namespace
          1. Remarks
          2. Example
          3. Output
          4. See Also
        2. using
          1. See Also
          2. using Directive
            1. Remarks
            2. Example
            3. Output
            4. Example
            5. Output
            6. See Also
          3. using Statement
            1. Remarks
            2. Example
            3. See Also
      8. Operator Keywords
        1. See Also
        2. as
          1. Remarks
          2. Example
          3. Output
          4. See Also
        3. is
          1. Remarks
          2. Example
          3. Output
          4. See Also
        4. new
          1. new Operator
            1. Example
            2. Output
          2. new Modifier
            1. Example
            2. Output
            3. Example
            4. Output
            5. See Also
        5. sizeof
          1. Remarks
          2. Example
          3. Output
        6. typeof
          1. Remarks
          2. Example
          3. Output
          4. Example
          5. Output
          6. See Also
        7. true
          1. true Operator
          2. true Literal
            1. Example
            2. Output
            3. See Also
        8. false
          1. false Operator
          2. false Literal
            1. Example
            2. Output
            3. See Also
        9. stackalloc
          1. Remarks
          2. Example
          3. Output
      9. Statement Keywords
        1. Selection Statements
          1. See Also
          2. if-else
            1. Remarks
            2. Example
            3. Input
            4. Sample Output
            5. Run #2:
            6. Run #3:
            7. Example
            8. Input
            9. Sample Output
            10. Run #2
            11. Run #3:
            12. Run #4:
            13. See Also
          3. switch
            1. Remarks
            2. Example
            3. Input
            4. Sample Output
            5. See Also
        2. Iteration Statements
          1. do
            1. Remarks
            2. Example
            3. Output
            4. Example
            5. Output
          2. for
            1. Remarks
            2. Example
            3. Output
          3. foreach, in
            1. Remarks
          4. Using foreach with Arrays
            1. Example
            2. Output
            3. See Also
          5. Using foreach with Collections
            1. Example 1
            2. Output
            3. Example 2a
            4. Output
            5. Example 2b
            6. Output
            7. Example 3
            8. Output
            9. See Also
          6. while
            1. Remarks
            2. Example
            3. Output
        3. Jump Statements
          1. break
            1. Example
            2. Output
            3. Example
            4. Input
            5. Sample Output
          2. continue
            1. Example
          3. goto
            1. Remarks
            2. Example
            3. Example
            4. Input
            5. Sample Output
            6. Example
          4. return
            1. Example
            2. Output
        4. Exception Handling Statements
          1. throw
            1. Remarks
            2. Example
            3. Output
            4. Example
          2. try-catch
            1. Remarks
            2. Example
            3. Output
            4. Example
            5. Output
            6. See Also
          3. try-finally
            1. Remarks
            2. Example
            3. Output
            4. See Also
          4. try-catch-finally
            1. Example
            2. Output
            3. See Also
        5. Checked and Unchecked
          1. checked
            1. Remarks
            2. Example 1
            3. Sample Output
            4. Example 2
            5. Output
            6. Example 3
            7. Output
            8. See Also
          2. unchecked
            1. Remarks
            2. Example 1
            3. Output
            4. Example 2
            5. Output
            6. Example 3
            7. Output
            8. See Also
        6. fixed Statement
          1. Remarks
          2. Example
          3. Output
          4. See Also
        7. lock Statement
          1. Remarks
          2. Example 1
          3. Output
          4. Example 2
      10. Type Keywords
        1. See Also
        2. Value Types
          1. Main Features of Value Types
          2. Main Features of Simple Types
          3. Initializing Value Types
            1. See Also
          4. bool
            1. Literals
            2. Conversions
            3. Example
            4. Input
            5. Sample Output
            6. See Also
          5. byte
            1. Literals
            2. Conversions
            3. See Also
          6. char
            1. Literals
            2. Conversions
            3. See Also
          7. decimal
            1. Literals
            2. Conversions
            3. Example 1
            4. Output
            5. Formatting Decimal Output
            6. Example 2
            7. Output
            8. See Also
          8. double
            1. Literals
            2. Conversions
            3. Example
            4. Output
            5. See Also
          9. enum
            1. Remarks
            2. Example 1
            3. Output
            4. Example 2
            5. Output
            6. See Also
          10. float
            1. Literals
            2. Conversions
            3. Example
            4. Output
            5. See Also
          11. int
            1. Literals
            2. Conversions
            3. See Also
          12. long
            1. Literals
            2. Conversions
            3. See Also
          13. sbyte
            1. Literals
            2. Conversions
            3. See Also
          14. short
            1. Literals
            2. Conversions
            3. See Also
          15. struct
            1. Remarks
            2. Example 1
            3. Output
            4. Example 2
            5. Output
            6. See Also
          16. uint
            1. Literals
            2. Conversions
            3. See Also
          17. ulong
            1. Literals
            2. Conversions
            3. See Also
          18. ushort
            1. Literals
            2. Conversions
            3. See Also
        3. Boxing and Unboxing
          1. See Also
          2. Boxing Conversion
            1. Example
            2. Output
            3. See Also
          3. Unboxing Conversion
            1. Example
            2. Output
            3. See Also
        4. Reference Types
          1. See Also
          2. class
            1. Remarks
            2. Example
            3. Output
            4. See Also
          3. delegate
            1. Remarks
            2. Example 1
            3. Output
            4. Example 2
            5. Output
            6. See Also
          4. interface
            1. Remarks
            2. Example
            3. Output
            4. See Also
          5. object
            1. Example
            2. Output
            3. See Also
          6. string
            1. Example
            2. Output
            3. See Also
          7. void
            1. See Also
        5. Types Reference Tables
          1. See Also
          2. Built-in Types Table
            1. Remarks
            2. See Also
          3. Integral Types Table
            1. Remarks
            2. See Also
          4. Floating-Point Types Table
            1. See Also
          5. Default Values Table
            1. See Also
          6. Value Types Table
            1. See Also
          7. Implicit Numeric Conversions Table
            1. Remarks
            2. See Also
          8. Explicit Numeric Conversions Table
            1. Remarks
            2. See Also
          9. Formatting Numeric Results Table
            1. See Also
    4. 3. C# Features
      1. Operators
        1. Arithmetic Overflow
          1. See Also
        2. Overloadable Operators
        3. [] Operator
          1. Remarks
          2. See Also
        4. () Operator
          1. Remarks
          2. Example
          3. Output
        5. . Operator
          1. Remarks
        6. + Operator
          1. Remarks
          2. Example
          3. Output
        7. - Operator
          1. Remarks
          2. Example
          3. Output
        8. * Operator
          1. Remarks
          2. Example
          3. Output
        9. / Operator
          1. Remarks
          2. Example
          3. Output
        10. % Operator
          1. Remarks
          2. Example
          3. Output
        11. & Operator
          1. Remarks
          2. Example
          3. Output
        12. | Operator
          1. Remarks
          2. Example
          3. Output
        13. ^ Operator
          1. Remarks
          2. Example
          3. Output
        14. ! Operator
          1. Remarks
          2. Example
          3. Output
        15. ~ Operator
          1. Remarks
          2. Example
          3. Output
        16. = Operator
          1. Remarks
          2. Example
          3. Output
        17. < Operator
          1. Remarks
          2. Example
          3. Output
        18. > Operator
          1. Remarks
          2. Example
          3. Output
        19. ?: Operator
          1. Remarks
          2. Example
          3. Output
        20. ++ Operator
          1. Remarks
          2. Example
          3. Output
        21. -- Operator
          1. Remarks
          2. Example
          3. Output
        22. && Operator
          1. Remarks
          2. Example
          3. Output
        23. || Operator
          1. Remarks
          2. Example
          3. Output
        24. << Operator
          1. Remarks
          2. Example
          3. Output
        25. >> Operator
          1. Remarks
          2. Example
          3. Output
        26. == Operator
          1. Remarks
          2. Example
          3. Output
        27. != Operator
          1. Remarks
          2. Example
          3. Output
        28. <= Operator
          1. Remarks
          2. Example
          3. Output
        29. >= Operator
          1. Remarks
          2. Example
          3. Output
        30. += Operator
          1. Remarks
          2. Example
          3. Output
        31. -= Operator
          1. Remarks
          2. Example
          3. Output
        32. *= Operator
          1. Remarks
          2. Example
          3. Output
        33. /= Operator
          1. Remarks
          2. Example
          3. Output
        34. %= Operator
          1. Remarks
          2. Example
          3. Output
        35. &= Operator
          1. Remarks
          2. Example
          3. Output
        36. |= Operator
          1. Remarks
          2. Example
          3. Output
        37. ^= Operator
          1. Remarks
          2. Example
          3. Output
        38. <<= Operator
          1. Remarks
          2. Example
          3. Output
        39. >>= Operator
          1. Remarks
          2. Example
          3. Output
        40. -> Operator
          1. Remarks
          2. Example
          3. Output
      2. Attributes
        1. See Also
        2. Introduction to Attributes
          1. See Also
          2. Using Attributes
            1. See Also
          3. Attribute Targets
            1. Example
            2. See Also
          4. Global Attributes
            1. Example
            2. See Also
          5. Creating Custom Attributes
            1. See Also
          6. Retrieving Attribute Information
            1. Example
            2. Output
            3. See Also
        3. AttributeUsage
          1. Parameters
          2. Applies To
          3. Remarks
          4. Example
          5. See Also
        4. Conditional
          1. Parameters
          2. Applies To
          3. Remarks
          4. Example
          5. Output
          6. See Also
        5. Obsolete
          1. Parameters
          2. Applies To
          3. Remarks
          4. Example
        6. Attributes Tutorial
          1. Further Reading
          2. Tutorial
          3. Declaring an Attribute Class
          4. Code Discussion
          5. Parameters for the AttributeUsage Attribute
          6. Using an Attribute Class
          7. Accessing Attributes Through Reflection
          8. Example
          9. Output
      3. Declarations
      4. Members
        1. Namespace Members
        2. Struct Members
        3. Class Members
        4. Other Members
        5. Member Access
        6. Declared Accessibility
        7. Accessibility Domains
        8. Protected Access for Instance Members
        9. Accessibility Constraints
      5. Preprocessor Directives
        1. See Also
        2. #if
          1. Remarks
          2. Example
          3. Output
        3. #else
          1. Remarks
          2. Example
        4. #elif
          1. Remarks
          2. Example
        5. #endif
          1. Remarks
          2. Example
        6. #define
          1. Remarks
          2. Example
          3. See Also
        7. #undef
          1. Remarks
          2. Example
          3. Output
        8. #warning
          1. Remarks
          2. Example
        9. #error
          1. Remarks
          2. Example
        10. #line
          1. Remarks
          2. Example
        11. #region
          1. Remarks
          2. Example
        12. #endregion
          1. Example
      6. Arrays
        1. See Also
        2. Single-Dimensional Arrays
          1. Array Initialization
          2. Value Type and Reference Type Arrays
          3. Passing Arrays as Parameters
          4. Example
          5. Output
          6. See Also
        3. Multidimensional Arrays
          1. Array Initialization
          2. Passing Arrays as Parameters
            1. Example
            2. Output
            3. See Also
        4. Jagged Arrays
          1. Example
          2. Output
          3. See Also
        5. Passing Arrays Using ref and out
          1. Example 1
          2. Output
          3. Example 2
          4. Output
        6. Arrays Tutorial
          1. Further Reading
          2. Tutorial
          3. Arrays in General
          4. Declaring Arrays
          5. Example
          6. Output
          7. Initializing Arrays
          8. Single-Dimensional Array
          9. Multidimensional Array
          10. Jagged Array (Array-of-Arrays)
          11. Accessing Array Members
          12. Arrays are Objects
          13. Using foreach on Arrays
      7. Constructors and Destructors
        1. Class Constructors
          1. Instance Constructors
            1. Remarks
            2. Example 1
            3. Output
            4. Example 2
            5. Output
            6. Example 3
            7. Output
            8. See Also
          2. Private Constructors
            1. Example
            2. Output
            3. See Also
          3. Static Constructors
            1. Remarks
            2. Example
            3. Output
            4. See Also
        2. Struct Constructors
          1. Example
          2. Output
          3. See Also
        3. Destructors
          1. Remarks
          2. Example
          3. Output
          4. Using Destructors to Release Resources
          5. Explicit Release of Resources
          6. See Also
      8. Indexers
        1. See Also
        2. Indexer Declaration
          1. The get Accessor
          2. The set Accessor
          3. Remarks
          4. Example
          5. Output
          6. See Also
        3. Comparison Between Properties and Indexers
          1. See Also
        4. Interface Indexers
          1. Remarks
          2. Example
          3. Output
          4. See Also
        5. Indexers Tutorial
          1. Further Reading
          2. Tutorial
          3. Example
          4. Input: indexer.txt
          5. Sample Output
          6. Code Discussion
      9. Properties
        1. See Also
        2. Property Declaration
          1. Remarks
          2. Example
          3. Output
          4. See Also
        3. Accessors
          1. The get Accessor
          2. The set Accessor
          3. Remarks
          4. Example 1
          5. Output
          6. Example 2
          7. Input
          8. Sample Output
          9. See Also
        4. Interface Properties
          1. Remarks
          2. Input
          3. Sample Output
          4. See Also
        5. Properties Tutorial
          1. Further Reading
          2. Tutorial
          3. Example 1
          4. Output
          5. Code Discussion
          6. Example 2
          7. File 1—abstractshape.cs
          8. Code Discussion
          9. File 2—shapes.cs
          10. File 3—shapetest.cs
          11. Output
      10. Signatures and Overloading
      11. Scopes
        1. General Rules for Scopes
        2. Name Hiding
        3. Hiding Through Nesting
        4. Hiding Through Inheritance
      12. Passing Parameters
        1. Passing Value-Type Parameters
          1. Example 1: Passing Value Types by Value
          2. Output
          3. Code Discussion
          4. Example 2: Passing Value Types by Reference
          5. Output
          6. Code Discussion
          7. Example 3: Swapping Value Types
        2. Passing Reference-Type Parameters
          1. Example 4: Passing Reference Types by Value
          2. Output
          3. Code Discussion
          4. Example 5: Passing Reference Types by Reference
          5. Output
          6. Code Discussion
          7. Example 6: Swapping Two Strings
          8. Output
          9. Code Discussion
          10. See Also
      13. Main Method
        1. Return Values
          1. Example
          2. Input
          3. Sample Output
          4. See Also
        2. Command-Line Arguments
          1. Example
          2. Output
          3. Run #1:
          4. Run #2:
          5. See Also
      14. Conditional Methods Tutorial
        1. Further Reading
        2. Tutorial
          1. Example
          2. File #1: Creating Conditional Methods
          3. Code Discussion
          4. File #2: Using the Conditional Method
          5. Code Discussion
          6. Sample Run
      15. Delegates Tutorial
        1. Further Reading
        2. Tutorial
          1. Example 1
          2. Output
          3. Code Discussion
          4. Example 2
          5. Output
          6. Delegates and Events
          7. Delegates vs. Interfaces
      16. Events
        1. Further Reading
          1. Tutorial
          2. Example 1
          3. Output
          4. Code Discussion
          5. Events and Inheritance
          6. Events in Interfaces
          7. .NET Framework Guidelines
            1. Example 2
            2. Output
      17. XML Documentation
        1. Tags for Documentation Comments
          1. <c>
            1. Remarks
            2. Example
          2. <code>
            1. Remarks
            2. Example
          3. <example>
            1. Remarks
            2. Example
          4. <exception>
            1. Remarks
            2. Example
          5. <include>
            1. Remarks
            2. Example
            3. Program Output
          6. <list>
            1. Remarks
            2. Example
          7. <para>
            1. Remarks
            2. Example
          8. <param>
            1. Remarks
            2. Example
          9. <paramref>
            1. Remarks
            2. Example
          10. <permission>
            1. Remarks
            2. Example
          11. <remarks>
            1. Remarks
            2. Example
          12. <returns>
            1. Remarks
            2. Example
          13. <see>
            1. Remarks
            2. Example
          14. <seealso>
            1. Remarks
            2. Example
          15. <summary>
            1. Remarks
            2. Example
          16. <value>
            1. Remarks
            2. Example
        2. Processing the XML File
          1. Examples
    5. A. C# Compiler Options
      1. Building from the Command Line
        1. Running VCVARS32.BAT
          1. To run VCVARS32.BAT
        2. Differences Between C# Compiler and C++ Compiler Output
        3. Rules for Command-Line Syntax
        4. Sample Command Lines
          1. See Also
      2. C# Compiler Options Listed by Category
        1. Optimization
        2. Output Files
        3. .NET Framework Assemblies
        4. Debugging/Error Checking
        5. Preprocessor
        6. Resources
        7. Miscellaneous
          1. See Also
      3. C# Compiler Options Listed Alphabetically
        1. See Also
      4. @ (Specify Response File)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
          3. Example
      5. /addmodule (Import Metadata)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
          3. Example
      6. /baseaddress (Specify Base Address of DLL)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
      7. /bugreport (Report a Problem)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
          3. Example
      8. /checked (Check Integer Arithmetic)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      9. /codepage (Specify Code Page for Source Code Files)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
      10. /debug (Emit Debugging Information)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      11. /define (Preprocessor Definition)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      12. /doc (Process Documentation Comments)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
      13. /filealign (Specify Section Alignment)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
      14. /fullpaths (Specify Absolute Path in Compiler Output)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
      15. /help, /? (Compiler Command-Line Help)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
      16. /incremental (Enable Incremental Compilation)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      17. /lib (Specify Assembly Reference Locations)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      18. /linkresource (Link to .NET Framework Resource)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
          3. Example
      19. /main (Specify Location of Main Method)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      20. /noconfig (Ignore csc.rsp)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
      21. /nologo (Suppress Banner Information)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
      22. /nostdlib (Do Not Import Standard Library)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
      23. /nowarn (Suppress Specified Warnings)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
      24. /optimize (Enable/Disable Optimizations)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      25. /out (Set Output File Name)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      26. /recurse (Find Source Files in Subdirectories)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
          3. Example
      27. /reference (Import Metadata)
        1. Remarks
          1. Example
      28. /resource (Embed Resource File to Output)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      29. /target (Specify Output File Format)
        1. /target:exe (Create a Console Application)
          1. Remarks
            1. To set this compiler option in the Visual Studio development environment
            2. Example
            3. See Also
        2. /target:library (Create a Code Library)
          1. Remarks
            1. To set this compiler option in the Visual Studio development environment
          2. Example
          3. See Also
        3. /target:module (Create Module to Add to Assembly)
          1. Remarks
            1. To set this compiler option in the Visual Studio development environment
          2. Example
          3. See Also
        4. /target:winexe (Create a Windows Program)
          1. Remarks
            1. To set this compiler option in the Visual Studio development environment
          2. Example
          3. See Also
      30. /unsafe (Enable Unsafe Mode)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      31. /utf8output (Display Compiler Messages with UTF-8)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
      32. /warn (Specify Warning Level)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      33. /warnaserror (Treat Warnings as Errors)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      34. /win32icon (Import an .ico File)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. Example
      35. /win32res (Import a Win32 Resource File)
        1. Remarks
          1. To set this compiler option in the Visual Studio development environment
          2. To set this compiler option programmatically
          3. Example
    6. B. Cordless Drill
    7. Index
    8. About the Author
    9. SPECIAL OFFER: Upgrade this ebook with O’Reilly