Skip to main content

C# Basics Revisited - Part I


Core Concepts Revisited: Points to remember


Classes

  •  Derived class implicitly contains all members of its base class, except constructors.
  •  A derived class cannot remove the definition of an inherited member, although it can extend the members.
  • An implicit conversion exists from a class type to any of its base class types.
For Example:
//Base Class
public class BaseClass
{
    public int x, y;
   
    public BaseClass(int x, int y)
      {
        this.x = x-1;
        this.y = y-1;
      }
    public int Sum()
    { return x + y ; }
   
}
//Derived Class
public class DerivedClass:BaseClass
{
    public int z;
      public DerivedClass( int x, int y,int z):base(x,y)
      {
        this.z = z-1;
      }

    public int Sum1()
    { return x + y + z; }
   

}

It is legal to call the  Sum function as follows:
BaseClass bc = new BaseClass();
int j=  bc.Sum();
OR
BaseClass bc1 = new DerivedClass(4, 5, 6);
int j=  bc1.Sum();

  • In a chain of derived classes, it is always the base class that gets instantiated first.
  • In case the base class constructor expects some parameters, the derived class needs to supply the parameters (as shown in derived class above) or the base class should implement a parameter less parameter.
Fields:

static field:
Marked with static keyword
It does not depend on the instances of class; there is only one copy of a static field ever.

instance filed:
Every instance of a class contains a separate copy of all the instance fields of that class.

read-only fields

These fields are marked with readonly modifier.
Assignment to a readonly field can only occur as part of the field’s declaration or in a constructor in the same class.

Methods:
Signature: The signature of a method does not include the return type.


Parameters:

value parameter:
A value parameter is used for input parameter passing. A value parameter corresponds to a local variable that gets its initial value from the argument that was passed for the parameter. Modifications to a value parameter do not affect the argument that was passed for the parameter.


reference parameter
Marked with ref keyword
A reference parameter is used for both input and output parameter passing.
Example:
public void Swap(ref int x, ref int y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
calling the function like this as shown below swaps the two numbers
       int i = 1, j = 2;
        dp.Swap(ref i, ref j);
        Response.Write(string.Format("{0},{1}",i,j));

Note here, we are not returning any values, the input and the output parameters are the same.
 
output parameter
Marked with the out modifier.
An output parameter is used for output parameter passing. An output parameter is similar to a reference parameter except that the initial value is not important. The following example shows the use of out parameters.

public void Result(int x, int y, out int additionresult, out int substractionresult)
    {
        additionresult = x + y;
        substractionresult = x - y;

    }
Call the method as follows:
int sumres,negation;
dp.Result(50, 40, out sumres, out negation);
Response.Write(string.Format("SumResult:{0} SubstractionResult{1}",sumres, negation));

parameter array

Permits a variable number of arguments to be passed to a method.
Declared with the params modifier
Only the last parameter of a method can be a parameter array
The type of a parameter array must be a single-dimensional array type.

Part II will cover some more basics..
Till Then Happy Coding!!

Comments

Popular posts from this blog

Asp.Net 4.0: An Overview-Part-III

This is the last post in the series which will explore the following new features of ASP.Net 4.0  Performance Monitoring for Individual Applications in a Single Worker Process Web.config File Refactoring Permanently Redirecting a Page Expanding the Range of Allowable URLs Performance Monitoring for Individual Applications in a Single Worker Process It is a common practice to host multiple ASP.NET applications in a single worker process, In order to increase the number of Web sites that can be hosted on a single server. This practice results in difficulties for server administrators to identify an individual application that is experiencing problems. ASP.NET 4 introduces new resource-monitoring functionality introduced by the CLR. To enable this functionality, following XML configuration snippet is added to the aspnet.config configuration file.(This file is located in the directory where the .NET Framework is installed ) <?xml version="1.0" encoding="UTF-8"

SOLID principles -Code Samples and Free Ebook

I planned to write code samples for SOLID principle implementations, however I am a firm believer of " NOT RE-INVENTING THE WHEEL ", when all you need is use the wheels and make a new CAR. Going by the ideology, I have found an excellent  SOLID principles FREE -Ebook ( covering all aspects of SOLID design principles, with Code sample). This book is an excellent visual aid to remember these principles, as it uses Motivational posters for explaining SOLID design principles. One additional advantage to the above mentioned book is the Code-Refactoring ebook . Both of these books can be downloaded from this EBOOK download Link Both of these books can be downloaded form here. Hope this book proves useful... Till next we connect.... Happy Learning..

Covariance and Contravariance-General Discussion

If you have just started the exploration of .Net Framework 4.0, two terms namely Covariance and Contravariance might have been heard. The concept that these terms encapsulate are used by most developer almost daily, however there has never been any botheration about the terminologies. Now, what actually these terms mean and how are these going to affect us as a developer, if we dive in to the details. The simple answer is it’s always good to know your tools before actually using them. Enough philosophy, let’s get to the business. Starting the discussion let me reiterate that in addition to Covariance and Contravariance, there is another terminology, Invariance. I’ll by start here by diving into the details of Invariance and then proceed further. Invariance: Invariance can be better understood by considering the types in .Net.>net has basically two type, value-types and reference-types. Value types (int, double etc) are invariant i.e. the types can’t be interchanged either