Some Useful and Important Concepts of C#

Introduction

In this article I am going to explain some basic and useful conceptsthat canhavetremendousvalue 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.

Type conversion in C#

There are only twomethods of type conversions

  1. Implicit
  2. Explicit

Implicit Type Conversion in C#

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

Example

int a=10; 
long b=a;

Explicit Type Conversion in C#

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

Example

int a=10; 
long b=a; 
int c=(int)b;

Boxing and Unboxing in C#


Boxing in C#

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

Example

int a=10; 
object b=a;

Unboxing in C#

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

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.

Type of methods in C#

  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();.

Example

console

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.

output

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.

Automatic method creation

We can achieve this with the help following way.

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

It seems as follows .

refractor


Recommended Free Ebook
Similar Articles