Wednesday, September 29, 2021

Write a program to reverse a string?


using System;


namespace ConsoleApp1

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "Hello";


            char[] revStr = new char[str.Length];


            int j = 0;

            //Reverse a string  

            for(int i=str.Length-1;i>=0;i--)

            {

                revStr[j++] = str[i]; 

            }


            Console.WriteLine("Reverse String: {0}", new String(revStr));

        }

    }

}



Friday, August 20, 2021

What is difference between Singleton and Static class?

 

This is interview question which is asked in many interviews.

Let’s see what Singleton is.

Singleton is design pattern, which allow user to create singleton instance of class.

    public sealed class Singleton

    {

        public static Singleton singleton = null;

        private Singleton(){} 

        public static Singleton GetSingletonInstance()

        {

            if (singleton == null)

            {

              return  new Singleton();

            }

            else

            {

                return null;

            }

        }

    }

Here Singleton is a class and it is marked as sealed, so it should not be

Inherited by any other classes. 

Constructor is private, so object of Singleton class cannot be created outside of this Singleton class.

GetSingletoninstance is method which returns instance of class.

Singleton:

  1. It can inherit any other class.
  2. It can override any methods.
  3. It can implement interface.
  4. Single Instance of class can be created.
  5. It can be passed to methods.
  6.  All methods are not static.

 

namespace ConsoleApp3

{

     //Interface which will be implemented by Singleton class

    public interface IInterface

    {

        public void SHow();

    }   

    // Singleton class inherit this MyClass

     public class MyClass

    {

        public void Disp()

        {

            Console.WriteLine("Disp");

        }

    }

     public sealed class Singleton: MyClass, IInterface

    {

        public static Singleton singleton = null;

        private Singleton(){}

         //All methods are not static. Disp() is non static method.

        public void Disp()

        {

            Console.WriteLine(Disp);

        }

         public static Singleton GetSingletonInstance()

        { 

            if (singleton == null)

            {

             return new Singleton();

            }

            else

            {

                return null;

            }

        }

         //Passing singleton instance to class

        public void Go(Singleton singleton)

        {

            singleton.Disp();

        }

    }

 Static Class:

  • Static class is Loaded by CLR.
  • It will be there in memory until application is running.
  • Instance of static class cannot be created.

See in below image, it is throwing error when I am trying to create instance of static class MyClass.




  • It cannot inherit any other class.
  •  

  • It cannot be inherited by any other class.





  • It cannot override method since static class cannot inherit any other class.
  • Static class cannot be passed to method parameters.
  • Instance of static class cannot be created, hence cannot be passed to method parameter.
  • It cannot be disposed.

 

 

Wednesday, August 18, 2021

How to restrict object creation to max 3 in .NET?

 This is interview question, which is asked in one of interview.

How you can allow to create only 3 object in C#.NET. It should throw error when user is trying to create more than 3 object.

Here is logic and program.


using System;

using System.Linq;

using System.Collections.Generic;

 namespace ConsoleApp16

{

     public class Student

    {

        static int count = 1;

        public Student()

        {

           if (count <= 3)

            {

                count = count + 1;

            }

            else

            {

                throw new Exception("you cannot create more than 3 instances");

                //count = 0;

            }

          

        }

         public int MyProperty { get; set; }

        public int MyProperty2 { get; set; }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            Student student = new Student();

            Student student2 = new Student();

            Student student3 = new Student();

            Student student4 = new Student();

           Console.WriteLine("");

        }

    }

}

 

Output:




Tuesday, August 17, 2021

How throw is different from throw ex?

 

Throw and throw ex both are used to throw exception caught in catch block.

Throw: It preserve the stack trace.

             Example: Method1 throw Error1 and caught by Method2 and again this method throws Error2

             StackTrace: Method1 Error1 + Method2Error2



using System;

using System.Collections.Generic;

 namespace ConsoleApp2

{

     class Program

    {

        static void Main(string[] args)

        {

            try

            {

                DevideNumberByZero(100);

            }

            catch (Exception ex)

            {

                throw;

            }

        }

         public static void DevideNumberByZero(int num)

        {

            try

            {

                Devide(num);

            }

            catch (Exception)

            {

                 throw;

            }

        }

        public static void Devide( int num)

        {

            try

            {

                int d = 0;

                int res = num / d;

                Console.WriteLine(res);

            }

            catch (Exception)

            {

                throw;

            }

        }

    }

}

 Here there are two methods Divide and DivideNumberByZero. Exception is thrown in Divide method and  it is caught by DivideNumberByZero, Exception is again throw by DevideNumberByZero method and it is caught by main method. You can see output in below. It is showing line number of all methods where exceptions were thrown.

Output:



Throw Ex: It does not preserve stack trace, so it will clear stack trace and will return only Method2Error2

              Example: Method1 throw Error1 and caught by Method2 and again this method throws Error2.


using System;

using System.Collections.Generic;

namespace ConsoleApp2

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                DevideNumberByZero(100);

            }

            catch (Exception ex)

            {

                throw ex;

            }

                 

        }

        public static void DevideNumberByZero(int num)

        {

            try

            {

                Devide(num);

            }

            catch (Exception ex)

            {

                throw ex;

            }

          

        }

              public static void Devide( int num)

        {

            try

            {

                int d = 0;

                int res = num / d;

                Console.WriteLine(res);

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

   }

}

 Here there are two methods Divide and DivideNumberByZero. Exception is thrown in Divide method and  it is caught by DivideNumberByZero, Exception is again throw by DevideNumberByZero method and it is caught by main method. You can see output in below. It is showing line number of only line 16 in  main method where exceptions is caught not all line numbers as shown in above output.

Output:



Write a program to reverse a string? using System; namespace ConsoleApp1 {     class Program     {         static void Main(string[] args)  ...