Tuesday, February 9, 2016

Lesson 4

Outline

Chapter 10

Override Methods
ToString()
Garbage Collection
Object Initializers

Override Methods

An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method.
The overridden base method must have the same signature as the override method.

You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.

An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

You cannot use the new, static, or virtual modifiers to modify an override method
.
An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.

class Person
{
      public virtual string SaveDay()
      {
          return "Day Saved";
      }
}
class Student : Person
{
      public override string SaveDay()
     {
          return "Day is Lost";
     }
}

In the above example we have two classes (Person and Student) The Person class contains a virtual method SaveDay that returns a string value "Day Saved". The Student class above inherits from Person. If we did nothing else the Student class would behave exactly like the Person class. However, in the Student class we have chosen to override the SaveDay() method. We can do this because of the virtual designation in the base class. By using the override designation we can change how the method behaves in the derived class. Note that the SaveDay() method in the Student class returns "Day is Lost". If we created two object based of these classes myPersom and myStudent, both would have the same methods but the method themselves would behave differently.

ToString()

Object.ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display.

Classes frequently override the Object.ToString method to provide a more suitable string representation of a particular type. Types also frequently overload the Object.ToString method to provide support for format strings or culture-sensitive formatting.

public class Vehicle
{
    public string Make { get; set; }
    public string Model { get; set; }
    public override string ToString()
    {
       return Make + " " + Model;
    }
}

In this class we have two properties to collect the make and model of our vehicle. We added the override ToString()method to change its behavior within the vehicle class.

static void Main(string[] args)
{
    Vehicle myCar = new Vehicle();
    myCar.Make = "Dodge";
    myCar.Model = "Charger";
    Console.WriteLine("My Car is a " + myCar.ToString());
}

Our implementation of the Vehicle class (myCar) has the properties set to Dodge and Charger. When we call the ToString() method of the object it will return "Dodge Charger" because we have changed the way the ToString() works in the Vehicle class.

Garbage Collection

The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.

Object Initializers

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.

In our myCar implementation above we set the value of the Make and Model class implicitly. In using object initializers we could do the following:

Vehicle myCar = new Vehicle() { Make = "Dodge", Model = "Charger" }

This is functionally the same as our previous sample but reduces the lines of code in our project. There are no hard and fast rules on when to use one approach over another. Choose which you are comfortable with and be consistent.







 

No comments:

Post a Comment