c#学习笔记

  1. C# Parameter Modifiers
    Parameter Modifier Meaning in Life
    (None) If a parameter is not marked with a parameter modifier, it is assumed to be
    passed by value, meaning the called method receives a copy of the original
    data.
    out Output parameters must be assigned by the method being called (and
    therefore are passed by reference). If the called method fails to assign
    output parameters, you are issued a compiler error.
    ref The value is initially assigned by the caller and may be optionally reassigned
    by the called method (as the data is also passed by reference). No compiler
    error is generated if the called method fails to assign a ref parameter.
    params This parameter modifier allows you to send in a variable number of
    arguments as a single logical parameter. A method can have only a single
    params modifier, and it must be the final parameter of the method.
  2. To define a nullable variable type, the question mark symbol (?) is suffixed to the underlying
    data type. Do note that this syntax is only legal when applied to value types. If you attempt to create
    a nullable reference type (including strings), you are issued a compile-time error. Like a nonnullable
    variable, local nullable variables must be assigned an initial value:
    static void LocalNullableVariables()
    {
    // Define some local nullable types.
    int? nullableInt = 10;
    double? nullableDouble = 3.14;
    bool? nullableBool = null;
    char? nullableChar = ‘a’;
    int?[] arrayOfNullableInts = new int?[10];
    // Error! Strings are reference types!
    // string? s = "oops";
    }
  3. The ?? Operator
    The final aspect of nullable types to be aware of is that they can make use of the C# ?? operator.
    This operator allows you to assign a value to a nullable type if the retrieved value is in fact null. For
    this example, assume you wish to assign a local nullable integer to 100 if the value returned from
    GetIntFromDatabase() is null (of course, this method is programmed to always return null, but I
    am sure you get the general idea):
    static void Main(string[] args)
    {
    Console.WriteLine("***** Fun with Nullable Data *****\n");
    DatabaseReader dr = new DatabaseReader();

    // If the value from GetIntFromDatabase() is null,
    138 CHAPTER 4 n CORE C# PROGRAMMING CONSTRUCTS, PART II
    // assign local variable to 100.
    int? myData = dr.GetIntFromDatabase() ?? 100;
    Console.WriteLine("Value of myData: {0}", myData.Value);
    Console.ReadLine();
    }
  4. Defining Static Classes
    Since the release of .NET 2.0, the C# language expanded the scope of the static keyword by introducing
    static classes. When a class has been defined as static, it is not creatable using the new
    keyword, and it can contain only members or fields marked with the static keyword
  5. Understanding Read-Only Fields
    Closely related to constant data is the notion of read-only field data (which should not be confused
    with a read-only property). Like a constant, a read-only field cannot be changed after the initial
    assignment. However, unlike a constant, the value assigned to a read-only field can be determined
    at runtime, and therefore can legally be assigned within the scope of a constructor (but nowhere
    else).
  6. The sealed Keyword
    C# supplies another keyword, sealed, that prevents inheritance from occurring. When you mark a
    class as sealed, the compiler will not allow you to derive from this type. For example, assume you
    have decided that it makes no sense to further extend the MiniVan class:
    // This class cannot be extended!
    sealed class MiniVan : Car
    {
    }
  7. Sealing Virtual Members

    sometimes you may not wish to seal an entire class, but simply want to prevent
    derived types from overriding particular virtual methods. For example, assume we do not want
    part-time salespeople to obtain customized bonuses. To prevent the PTSalesPerson class from overriding
    the virtual GiveBonus() method, we could effectively seal this method in the SalesPerson
    class as follows:
    // SalesPerson has sealed the GiveBonus() method!
    class SalesPerson : Employee
    {

    public override sealed void GiveBonus(float amount)
    {

    }
    }
    Here, SalesPerson has indeed overridden the virtual GiveBonus() method defined in the
    Employee class; however, it has explicitly marked it as sealed. Thus, if we attempted to override this
    method in the PTSalesPerson class:
    202 CHAPTER 6 n UNDERSTANDING INHERITANCE AND POLYMORPHISM
    sealed class PTSalesPerson : SalesPerson
    {
    public PTSalesPerson(string fullName, int age, int empID,
    float currPay, string ssn, int numbOfSales)
    :base (fullName, age, empID, currPay, ssn, numbOfSales)
    {
    }
    // No bonus for you! Error!
    public override void GiveBonus(float amount)
    {
    // Rats. Can’t change this method any further.
    }
    }
    we receive compile-time errors.

  8. Obtaining Interface References: The as Keyword
    The second way you can determine whether a given type supports an interface is to make use of the
    as keyword, which was first introduced in Chapter 6. If the object can be treated as the specified
    interface, you are returned a reference to the interface in question. If not, you receive a null reference.
  9. Obtaining Interface References: The is Keyword
  10. The unsafe Keyword
    When you wish to work with pointers in C#, you must specifically declare a block of “unsafe code”
    using the unsafe keyword (any code that is not marked with the unsafe keyword is considered “safe”
    automatically). For example, the following Program class declares a scope of unsafe code within the
    safe Main() method:
    class Program
    {
    static void Main(string[] args)
    {
    unsafe
    {
    // Work with pointer types here!
    }
    // Can’t work with pointers here!
    }
    }

1 thought on “c#学习笔记”

  1. Great site. A lot of useful info here. I’m sending it to several buddies ans also sharing in delicious. And of course, thank you on your effort!

Comments are closed.