Some Useful and Important Concepts of C#

INTRODUCTION :

In this article I am going to explain some basic and useful concepts that can have tremendous value from a programming point of view in C#. Basically in this article I have explained type of conversion i.e. explicit and implicit, Boxing and Unboxing of data types, Static and NonStatic methods and the technique of creating automatic methods.

A. Type conversion

There are only two methods of type conversions:

  1. Implicit
  2. Explicit

1. Implicit conversion: By default conversion is known as implicit conversion which means a lower to a higher conversion is Implicit conversion.

e.g.  int a=10;
       long b=a;

2. Explicit conversion: Higher to lower conversion is known as Explicit conversion. Type casting is necessary for Explicit conversion.

e.g.  int a=10;
       long b=a;
       int c=(int)b;

B. Boxing and Unboxing

  1. Boxing : In Boxing a value type data type is converted to a reference type or object type.

    e.g. int a=10;
          object b=a;

  2. Unboxing : In Unboxing a reference type data type is converted into a value type.

    e.g. int a= 10:
           object b=a;
           int c=(int)b;

NOTE : Type casting is necessary for Unboxing.

Take a look at the following example:

   int a=10;
   object b=a;
   long d=(long)b;

This is not possible because b has an integer value.

NOTE : We can unbox only variables which have previously being boxed.

  1. IntParse is used in relative method Convert.ToInt32 or char is used in global conversion in all languages in .NET.
  2. Parse only applied for readline type value but convert any type into any type.
  3. Parse is structure but Convert is class.
  4. Convert used globally in all language.

C. Type of method

  1. Static method : No need to be create a class object; we can directly call the method with the help of class name i.e. classname.method();
  2. Nonstatic method : First of all we have to create a class object after that we can call the method with object name i.e. objectname.method();

For example : Take a console application as follows:

m1.jpg

The code looks like:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication18
{
class Program
{
    static void Main(string[] args)
      {
            x.show();
            x obj = new x();
            obj.show1();
            Console.Read();
       }

}
    class x

    {
        public static void show()
       {
         Console.WriteLine("Hello");
       }

        public void show1()
        {
         Console.WriteLine("hello1");
        }

   }
}

When we run this program the output would like this:

fig1.jpg

Hence we can call the show() method with the help of class name whereas the show1() method can be called with the help object because show1() isn't declared as static.  

D. Automatic method creation :

We can achieve this with the help following way:

<Select Code - Right Click - Refactor - Extract Method >   

It seems as follows :

fig3.jpg
 

Up Next
    Ebook Download
    View all
    Learn
    View all