C#.NET Encapsulation

  • Encapsulation is a procedure of binding data under class name.
  • Encapsulation is a procedure of covering up of the data & functions into a single unit called as Class.
  • An encapsulated object is often called an abstract data type.
  • Rather than defining the data in the form of public, we can declare those fields as private.
  • In other words wrapping up of data and methods into a single unit is called as "Encapsulation"(ex:Class,method).
Tuesday, 5 January 2016
Posted by Sudhir Chekuri
Tag :

C#.NET Partial class

Partial class is an unfinished class.

Generally we cannot create two classes with the same name in a namespace. So, Every class you create is a finished class which cannot open again to add some code.
If you create a class as partial class then you can again create a class with the same name and the code inside both the classes will belong to the same class.

Example program to create partial classes and calling methods inside two partial classes with the same object

Program
using System;

namespace PartialClass
{
    class Program
    {
        static void Main(string[] args)
        {
            a o = new a();
            o.add();
            o.sub();
            Console.ReadKey();
        }
    }

    partial class a
    {
        public void add()
        {
            Console.WriteLine("add");
        }
    }
    partial class a
    {
        public void sub()
        {
            Console.WriteLine("sub");
        }
    }

}


Output

add

sub
Posted by Sudhir Chekuri
Tag :

C#.NET Destructor

  • · Destructor is a special method which has the same name of the class.
  • · It is invoked automatically when an object of that class is cleared.
Example program to declare a destructor for a class

Program

using System;
namespace destructor
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.ReadKey();
        }
        ~Program()
        {
            Console.WriteLine("destroyed");
            Console.ReadKey();
        }
    }
}


Output
destroyed

In the above example destructor with the same name of the class i.e., Program is created using tilt symbol before the method name as below.

~ Program()
{
}

This destructor is invoked automatically when the memory of that class Program is cleared.
So, the message in destructor method is displayed before the output screen gets closed.
Posted by Sudhir Chekuri
Tag :

C#.NET Constructor - with parameter, overloading

  • Constructor is a special method which has the same name of the class.
  • It is invoked automatically when an object for that class is created.
  • It is used for initializing the components of a class.
Example program to declare a constructor for a class
Program

using System;
namespace constructor
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.ReadKey();
        }
        public Program()
        {
            Console.WriteLine("program");
        }
    }
}

Output
program

In the above example a constructor with name Program is declared i.e., with the same name of the class in which it is declared.
This constructed is invoked automatically when an object is created for the Program class.
So, When an object p is created for the class Program constructor is invoked and the code inside that gets executed to print program on the output screen

Constructor with parameter

Declaring a user defined constructor with and input parameter is known as Constructor with parameter or parameterized constructor.

Example program to declare a parameterized constructor for a class

Program


using System;
namespace constructor
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program(5);
            Console.ReadKey();
        }
        public Program(int i)
        {
            Console.WriteLine("Parameter value: "+i);
        }
    }
}

Output

Parameter value: 5

Constructor Overloading

Multiple constructors for the same class with the same names but different parameters is known as constructor overloading.

Example program for constructor overloading

using System;
namespace constructor
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program(5);
            Program p1 = new Program(5, 10);
            Console.ReadKey();
        }
        public Program(int a)
        {
            Console.WriteLine("Parameter value: " + a);
        }
        public Program(int a, int b)
        {
            Console.WriteLine("Sum: " + (a + b));
        }
    }
}


Output

Parameter value: 5

Sum: 15
Posted by Sudhir Chekuri
Tag :

C#.NET Object

  • Object is an instance of class.
  • Object will have memory.
  • Object is used to call methods of a class.
  • Object is a reference type.
  • To create this object "new" keyword is used.
Syntax

classname objectname= new constructor();

Example

maths m=new maths();

Example program using object for a class

Program

using System;

namespace Class_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            maths m = new maths();
            m.add();
            Console.ReadKey();
        }
    }
    class maths
    {
        public void add()
        {
            int a = 10;
            int b = 20;
            Console.WriteLine("Sum is: " + (a + b));
        }
    }
}

Output

sum is : 30
Posted by Sudhir Chekuri
Tag :

C#.NET Class

  • Class is a blue print.
  • It is a collection of methods and member functions.
  • In Object oriented programming every line of code should be in class.
  • It is a reference type.
  • Mandatory to use the "new" keyword for creating object.
  • Class can contain variable declarations as well as initialize them at the time of declaration only.
Syntax

class classname

{

body

}

Thursday, 31 December 2015
Posted by Sudhir Chekuri
Tag :

C#.NET Static Method

Static method is a method which can be called without any reference or object because memory for a static method is created in RAM when program is executed.

Static method is a method which is created using keyword "static".

Example

Addition of two numbers Program using static method and takes input arguments with return type

Program

using System;

namespace static_method
{
    class Program
    {
        static void Main(string[] args)
        {
            long l = maths.add(4, 7);
            Console.WriteLine(l);
            Console.ReadKey();
        }
        class maths
        {
            public static long add(int i, int j)
            {
                return i + j;
            }
        }
    }
}


Output

11
Posted by Sudhir Chekuri
Tag :

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -