Sealed class in c# with real time example

Sealed classes and Sealed methods in C#.NET

Introduction of Sealed class:A sealed class is a class that does not allow inheritance. Some object model designs need to allow the creation of new instances but not inheritance, if this is the case, the class should be declared as sealed.To create a sealed class in C#, the class declaration should be done as:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
sealed class SealedClass   // Sealed class
{
    public int Add(int x, int y)
    {
        return x + y;
    }
} 
class Class1 : SealedClass
{
    static void Main(string[] args)
    {
        SealedClass sealedCls = new SealedClass();
      //Object creation of sealed class
        int total = sealedCls.Add(4, 5);                  
      //Calling the function
        Console.WriteLine("Total = " + total.ToString());
    }
}

When you try to derive a class from sealed class compiler throw an error."'Class1': cannot derive from sealed type 'SealedClass' "





The actual mean of using sealed class is to prevent inheritance of a class, we do not want to allow some class  to extend the functionality of a class.

Advantages of sealed class in c# 
1.Sealed keyword in C# applies restrictions on the class and method
2.At class level sealed modifier prevents a class from being inherited. 

Some true fact about a sealed class in c#

* Can we create instance of sealed class in c#
   Yes, you can

* Purpose of sealed class in c# net
  Is to prevent class being inherit

* Sealed class is like the normal class but the sealed class can't inherited but it's possible can be instantiated like normal class.

* To declare sealed class we should use the sealed keyword with the class declaration.

* The sealed classes are useful in order to protect our classes not to be used by any other classes.


Sealed Method:
We can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. If we want to stop the derivation of class for the next level then we need to mark method as sealed in previous class.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SealedClass
{
    class Boss
    {
        virtual public void Work()
        {
            Console.WriteLine("In class A");
        }
    }

    class TL : Boss
    {
        sealed override public void Work()
        {
            Console.WriteLine("In class B");
        }
    }
    class Employee : TL
    {
        override public void Work() //Error: cannot override inherited member 
        {                               //TL.Work() because it is sealed  

        }
    }
}


 Also Read:






Read More

Hello world program in typescript

Hello world program

Today I am going to show you how to write simple hello world program in typescript, it is very simple to write helloword program and run it. Also we will talk about how to write a class in typescript and constructor in typescript.

Open the vs code and type the following single line of code and save it in your system's folder and follow the given steps.

console.log("Hello Word");

Step to compile and run the typescript program.

1.Save it with name helloword.ts
2.Open NodeJs Command Prompt , go to the directory where your helloword.ts file is saved.
3.In my case file available in E:\typescriptprtcl\New folder
4.So I am trying to go to in E:\typescriptprtcl\New folder using command.
     
Hello world program in typescript




5.Now do the following steps

1. tsc helloword.ts  when you type this command and hit enter the typescript program will be compile and one more file created with the name helloword.js  .See in your folder,here tsc means typescript compiler.
2. node helloword.js whenever you hit this command your helloword.js result show in picture.


Note:

tsc is compiler which convert the typescript file into javascript file so that we can run javacript, we can’t run directly typescript first we need to convert it.


Hello world program in typescript



How to write class in typescript and create object of class

class office
{
   empcode:any  ="Helloword";
}
   let obj=new office();
   console.log(obj.empcode);

In above example any is a variable which is able to store any datatype in typescript. Let key use to create an object of a class so that we can call variable of the class. console.log  is same as Console.Write function in c#.

Constructor In typescript:

Constructor is special type of function which name same as class name in other language.  But in typescript if we want to create constructor , we need to write constructor keyword. See Example



class office
{
   empcode:any;
   constructor(code:number)
   {
     this.empcode=code;
   }   
}
   let obj=new office(101);
   console.log(obj.empcode);



Read More

[Solved] Random password generator c# with special characters

Strong password generator c#

Hey, Today I am going to show you how you can generate strong password with special characters in c#. Here we are taking a console application to do that

Random password generator c# with special characters

               Strong password generator c#


                                       How to upload image in gridview in asp.net using c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Random_password
{
    class Program
    {
        static string alphaCaps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        static string alphaLow = "abcdefghijklmnopqrstuvwxyz";
        static string numerics = "1234567890";
        static string special = "@#$-=/";
        string allChars = alphaCaps + alphaLow + numerics + special;
        Random r = new Random();

        public string GenerateRandomStrongPassword(int length)
        {
            String generatedPassword = "";
            if (length < 4)
                throw new Exception("Number of characters should be greater than 4.");
            int lowerpass, upperpass, numpass, specialchar;
            string posarray = "0123456789";
            if (length < posarray.Length)
            posarray = posarray.Substring(0, length);
            lowerpass = getRandomPosition(ref posarray);
            upperpass = getRandomPosition(ref posarray);
            numpass = getRandomPosition(ref posarray);
            specialchar = getRandomPosition(ref posarray);


            for (int i = 0; i < length; i++)
            {
                if (i == lowerpass)
                    generatedPassword += getRandomChar(alphaCaps);
                else if (i == upperpass)
                    generatedPassword += getRandomChar(alphaLow);
                else if (i == numpass)
                    generatedPassword += getRandomChar(numerics);
                else if (i == specialchar)
                    generatedPassword += getRandomChar(special);
                else
                    generatedPassword += getRandomChar(allChars);
            }
            return generatedPassword;
        }

        private string getRandomChar(string fullString)
        {
            return fullString.ToCharArray()[(int)Math.Floor(r.NextDouble() * fullString.Length)].ToString();
        }

        private int getRandomPosition(ref string posArray)
        {
            int pos;
            string randomChar = posArray.ToCharArray()[(int)Math.Floor(r.NextDouble()* posArray.Length)].ToString();
            pos = int.Parse(randomChar);
            posArray = posArray.Replace(randomChar, "");
            return pos;
        }
        
        //Calling the method 
        static void Main(string[] args)    //Main Method
        {
            Program p = new Program();
            string rs = p.GenerateRandomStrongPassword(8);
            Console.WriteLine(rs);
        }
    }
}

Compile and run the program you will get the result. If you want to use it in your project then call the function and pass the password length in parameter()

How to upload image in gridview in asp.net using c#
How to perform insert and update operation on single button
Stored procedure to insert data into multiple tables in sql server



Read More