Showing posts with label Commenting in C#. Show all posts
Showing posts with label Commenting in C#. Show all posts

Using comments in C#

Chapter 3,

USING COMMENTS


It is a best practice to use comments where ever possible in your code, comments help you in documenting the program so that in some later point when you review your code you actually remember what the code does and what is the logic behind the code.
Also it so happens that, applications are developed by a large team and the module/code written by you will be used by someone else to write another logic/program. The understanding of your code is greatly helped by your comments, and the time required to develop the application is greatly reduced.
Comments/Documentation also helps in maintenance of the application.

In C# there are two ways of commenting your code, the traditional C-Type inline single line comments(// ..)
and Multi-line comments (/* .. */)

Single Line comments:
using the single line inline comment only a single line is commented.
For example.

// int i;  this line is commented
   int j;

whenever the compiler encounters the two forward slashes , it ignores all the remaining characters following the slashes until the end of the line.

Multi Line comments:
Using the multi line commenting method , more than one line of code can be commented.
For example.

/* int i ;
    int j;
    int k;
*/

whenever the compiler encounters the forward slash and a Asterisk ( /* ) the compiler ignores all the characters until it encounters a asterisk followed by a forward slash ( */ ) which denotes the end of comment.

It is possible to include a multi line comments within a single line.








Inheritance and polymorphism in C#

Chapter 5,

INHERTITANCE AND POLYMORPHISM 
One of the most important concept of object oriented programming languages is inheritance.
inheritance helps in reusing the code, hence helping the programmer save time in the coding process by making use of the already written logic/code in multiple situations

The class inheritance:
Inheritance in class level may be represented in the following way.
A base class which contains fields, methods etc, can be inherited into a derived class, and the derived class will contain all the fields / methods defined in the base class.

For example:

using System;

namespace Logiphix
{
         public class BaseClass
        {
                  public BaseClass()
                  {
                          Console.WriteLine("BaseClass Object created");
                  }

                  public void printMessage()
                  {
                          Console.WriteLine("This method was defined  inside BaseClass");
                  }
         }

         public class DerivedClass : BaseClass
         {
                    public DerivedClass ()
                    {
                             Console.WriteLine("DerivedClass Object created");
                    }
          }

          public class InheritanceExample
          {
                   static void Main()
                   {
                           DerivedClass  dcObj = new  DerivedClass();
                           dcObj.printMessage();
                           Console.ReadKey();
                   }
          }
}

Explanation:

The above example defines three classes, namely BaseClass , DerivedClass, InheritanceExample.

The BaseClass will act as the parent class in this example and this class has a method defined in it called printMessage  this method will print an message on the console as "This method was defined inside BaseClass" 

The DerivedClass will act as the child class and inherits the parent class BaseClass, when doint so it also inherits all its member methods and fields. hence inheriting the method printMessage as well.

the third class InheritanceExample contains the main method for this application and creates an object of the class DerivedClass  when the object is created using the new keyword , note that the base class constructor is called first and then the derived class constructor is called.

Finally when the printMessage method is called with respect to the derived class object, the method is succesfully executed, even though the method was defined inside the base class and not inside the dreived class. this shows us that the method was inherited from the BaseClass(which is the parent class) into the DerivedClass class(which becomes the child class)

The output for the code is as below