Section 7.9. Summary


7.9. Summary

  • When you define a new class, you declare its name with the class keyword, and then define its methods , fields, delegates, events, 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 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 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 can see and use a variable or method within a class. All members of the class are visible to all methods of the class.

Structs

Structs are value types, but they are similar to classes in that they can contain constructors, properties, methods, fields, and operators, all explained in this chapter. Structs can also support indexers (see Chapter 12).

On the other hand, structs don't support inheritance or destructors (see Chapter 11) or field initialization. You define a struct almost exactly like you define a class:

 [   attributes   ] [   access-modifiers   ] struct   identifier   [:   interface-list   ] {   struct-members   } 

Structs implicitly derive from Object (as do all types in C#, including the built-in types) but cannot inherit from any other class or struct (as classes can). Structs are also implicitly sealed (that is, no class or struct can derive from a struct) (see Chapter 11); this is not true for classes.

The goal of structs is to be "lightweight"requiring little memory overheadbut their use is so constrained, and the savings are so minimal, that most programmers make little use of them.

C++ programmers beware : structs in C++ are identical to classes (except for visibility)that is not true in C#.


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

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

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

  • 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.

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

  • Every non-static 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 destructor 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 references 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.



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net