Tuesday 28 April 2015

What is Partial class in asp.net c# ?


Partial Class :
We were declaring a class in a single file but Partial class is a feature which allows us to write class across multiple files.
The partial indicates that the parts of the class, struct, or interface can be defined in the namespace. All the parts must be used with the partial keyword. All the parts must be available at compile time to form the final type or final class. All the parts must have the same accessibility level, such as public, private, protected, and so on.
If any part of the class is declared abstract, then the whole type is considered to be as abstract.
If any part is declared sealed, then the whole type is considered to be as sealed.

If any part declares a base type, then the whole type inherits that class.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data;

   
namespace PartialClass
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Console.WriteLine("Please enter first number");
            int a = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter second number");
            int b = int.Parse(Console.ReadLine());
            MyTest ts = new MyTest();
            ts.getAnswer(a, b);
            ts.PrintCoOrds();
            Console.Read();
        }
    }
}

MyTest1.cs
namespace PartialClass
{
    public partial class MyTest
    {
        private int a;
        private int b;

        public void getAnswer(int a, int b)
        {
            this.a = a;
            this.b = b;
        }
    }
}

MyTest2.cs
namespace PartialClass
{
    public partial class MyTest
    {
        public void PrintCoOrds()
        {
            Console.WriteLine("Integer values are : {0}, {1}\n", a, b);
            Console.WriteLine("Addition of numbers: {0}\n", a + b);
            Console.WriteLine("Mulitiply of numbers : {0}\n", a * b);
        }
    }
}
      


Output:-

















Advantages
More than one Programmer or developer can simultaneously write the code for class.
Partial class allows a clean separation of business logic layer and the user interface.

Partial Method 
Partial class or struct can contain Partial method.
One part of class contains signature or declaration of the method and the implementation or definition of method can be in same class or different class.
Partial methods enable the implementer of one part of a class to define a method, similar to an event. The implementer of the other part of the class can decide whether to implement the method or not. If the method is not implemented, then the compiler removes the method signature and all calls to the method .
A partial method declaration consists of two parts: 
1. definition and 
2. Implementation.

// Definition in file1.cs
partial void onTextChanged(); // Implementation in file2.cs partial void onTextChanged() { // method body }


Points:-
  1. Partial methods can have ref but not out parameters.
  2. Partial method can have static or unsafe modifiers but can not be extern as presence of body decide whether they are defining or implementing.
  3. Partial method can be Generic.
  4. Partial methods are implicitly private, and therefore they cannot be virtual.



What is Sealed class in asp.net c# ?

Sealed Class :-


Classes can be declared as sealed. This is accomplished by putting the sealed keyword before the keyword class in the class definition Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as sealed class, this class cannot be inherited. A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.

Sorce code example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data;

namespace ConsoleDemoApp
{
    class MySealed
    {
        /* program to implement Sealed class in c# */

      
           static void Main(string[] args)
            {
                SealedClass sealedCls = new SealedClass();
                Console.WriteLine("Please enter first number");
                int a = int.Parse(Console.ReadLine());
                Console.WriteLine("Please enter second number");
                int b = int.Parse(Console.ReadLine());
                int total = sealedCls.Add(a, b);
                Console.WriteLine("Total = " + total.ToString());
                Console.ReadKey();
            }

      
    }

    // Sealed class example
     sealed class SealedClass
     {
        public int Add(int x, int y)
        {
            return x + y;
        }
     }
 }

Potput














Sealed Method :

When an instance method declaration includes a sealed modifier, that method is said to be a sealed method. A sealed method overrides an inherited virtual method with the same signature. A sealed method shall also be marked with the override modifier. Use of the sealed modifier prevents a derived class from further overriding the method.

Source code

class AA
    {
        public virtual void First()
        {
            Console.WriteLine("First Class AA");
        }
        public virtual void Second()
        {
            Console.WriteLine("Second Class AA");
        }
    }
    class BB : AA
    {
        public sealed override void First()
        {
            Console.WriteLine("First Class BB");
        }
        public override void Second()
        {
            Console.WriteLine("Second Class BB");
        }
    }
    class CC : BB
    {
        public override void Second()
        {
            Console.WriteLine("First Class CC");
        }
    }


Note :-

Below method can be further override

public sealed override void First()
        {
            Console.WriteLine("First Class BB");
        }