Summary

  • When you define a new class, you declare its name with the class keyword, and then define its methods, fields, and properties.

  • To instantiate an object, you declare the name of the class, followed by an identifier for the object, much as you would a local variable. You then need to allocate memory for the actual (unnamed) object that will be created on the heap; you do so with the keyword new.

  • You invoke a method on an object by writing the name of the object, followed by the dot operator, and the method name followed by parentheses. Parameters, if any, are placed within the parentheses.

  • Access modifiers dictate which methods of external classes can see and use a variable or method within a class. All members of the class are visible to all methods of its own class.

  • Members marked public have no restrictions, and are visible to methods of any class.

  • Members marked private are visible only to methods within the same class.

  • Members marked protected are visible to methods within the same class, and methods in derived classes.

  • If you know the return type of a method, you can use a method call anyplace you would use an instance of that type.

  • A constructor is a special method invoked when a new object is created. If you do not define any constructors at all for your class, the compiler will provide a default constructor that does nothing. A default constructor is a constructor that takes no parameters. You are free to create your own default constructor for your class.

  • You can initialize the values of your member variables when you define them in your class.

  • Object initializers allow you to set the public fields of an object immediately after you create the object.

  • Anonymous types allow you to create a class with no name, and initialize its fields immediately. The compiler will implicitly assign types to those fields. You can use the var keyword to create an instance of the anonymous object.

  • The this keyword is used to refer to the current instance of an object.

  • Every nonstatic method of a class has an implicit this variable passed into the method.

  • Static members are associated with the class itself, not with a particular instance. Static members are declared with the keyword static, and are invoked through the class name. Static methods do not have a this parameter because there is no instance to refer to.

  • C# does not specifically require a finalizer method in your classes because the framework will destroy any object that is not in use.

  • You should provide a Dispose( ) method if your class uses unmanaged resources.

  • Local value type variables are created on the stack. When the method ends, these variables go out of scope and are destroyed.

  • Objects are reference types, and are created on the heap. When you declare an instance of a reference type, you are actually creating a reference to that object’s location in memory. If you declare a reference to an object on the heap within a method, when the method ends that reference is destroyed. If there are no remaining references to the object on the heap, the object itself is destroyed by the garbage collector at some later time.

  • You can define a reference to an existing object by declaring the class and an identifier and then assigning to that identifier an existing object; the two identifiers now both refer to the same (unnamed) object on the heap.

You spent the preceding chapter learning the theory, and now you’ve seen some of the practice behind the most powerful concept in C#. Hopefully by this point, you’ve seen how you can model just about anything with carefully defined classes and methods. You may have noticed a few limitations of the methods we’ve shown you so far, though. For example, methods can return only a single value—what if you want to manipulate and return two or more values? Or what if you’re not quite sure how many parameters you’ll have when you call the method? Maybe you’ll create a Dog object with a name and a weight, but perhaps sometimes you just have the name, and you’ll need to add the weight later. C# methods are flexible enough to handle all of these cases, and in the next chapter, you’ll spend time looking at them more closely.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset