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

Authentication using Social Networking portals(Facebook, Gmail, and Yahoo)

There are tons of sites, which offer sign on using the social networking site credentials (Facebook, gtalk, twitter and the list continues).It can be termed a “SINGLE SIGNON” and offers a lot of benefits compared to traditional database authentication approach. However, not storing user credentials in the DB imposes an additional risk. How to track who all logged into the system. Now the question is, which approach to follow. The best approach is to use inbuilt asp.net users for storing the logging info about user activities and using single signon technique for authentication. This article will explore the approach and provide the details of implementation using some third party libraries and customizing it to the requirements. The Authentication will be done using the following networking portals Yahoo  Gmail Facebook    Special thanks to my friend Sumit Khandelwal, for implementation of Facebook part (in fact he did it all!!) Except Facebook, all other ca...

WPF Overview-Part-II

This post is in continuation to the last post. In this Post I’ll be exploring the Dependency Properties Dependency properties are similar to CLR properties with more advanced and complex features. The main difference between the CLR properties and dependency properties is, that the value of a normal .NET property is read directly from a private member in your class, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject . In case this description did not make sense, no need to worry, It will become clear by the time you reach end of this article. How the Value is Resolved in Dependency properties Every time a dependency property is accessed, it internally resolves the value by following the precedence from high to low. It checks if a local value is available, if not, check if a custom style trigger is active and I the similar manner continues until it finds a value. At last the default value is alwa...

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