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

WCF-REST Services-Part-II

HOW REST is implemented in WCF Part-I of the series explored the REST conceptually and this post will explore how REST is implemented in WCF. For REST implementation in WCF, 2 new attributes namely WebGetAttribute and WebInvokeAttribute are introduced in WCF along with a URI template mechanism that enables you to declare the URI and verb to which each method is going to respond. The infrastructure comes in the form of a binding ( WebHttpBinding ) and a behavior ( WebHttpBehavior ) that provide the correct networking stack for using REST. Also, there is some hosting infrastructure help from a custom Service¬Host ( WebServiceHost ) and a ServiceHostFactory ( WebServiceHostFactory ). How WCF Routes messages WCF routes network messages to methods on instances of the classes defined as implementations of the service. Default behavior ( Dispatching ) for WCF is to do this routing based on the concept of action. For this dispatching to work, an action needs to be present in ev...

WCF..Why Communication Foundation?

WCF (Basics..for building effective Services) WCF:  Windows Communication Foundation It includes a collection of of .NET distributed technologies that have existed for long , but never got grouped under one name. WCF can be considered as collection of the following technologies. Web Services(ASMX) NET Enterprise Services MSMQ .NET Remoting Code written in WCF can interact across components, applications and systems. WCF is in accordance with SOA (Service Oriented Architecture). Following Sections provide the details of these ABCs. Addresses In WCF, every service has a unique address. The address provides two important elements A)     Location of the service B)      Transport protocol or transport schema used to communicate with the service.  The location indicates the name of the target machine, site, or network; a communication port, pipe, or queue; and an optional specific path or URI. WCF supports th...