Saturday, 27 March 2021

In this article, we will see How to find Occurrences of a character in a String C# ? 
There are multiple ways to find the Occurrences of a character in a String C# however we need to use best and optimized performance oriented way to achieve this functionality. 

Using Basic Approach/Logic: 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace count_occurence_of_char_using_basic_logic
{
    class Program
    {
        static void Main(string[] args)
        {
            string _str = "occurences";
            Console.WriteLine("String: " + _str);
            while (_str.Length > 0)
            {
                Console.Write(_str[0] + " = ");
                int count = 0;
                for (int j = 0; j < _str.Length; j++)
                {
                    if (_str[0] == _str[j])
                    {
                        count++;
                    }
                }
                Console.WriteLine(count);
                _str = _str.Replace(_str[0].ToString(), string.Empty);
            }
            Console.ReadLine();
        }
    }
}


Using Dictionary:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Count_occurences_of_char_using_dictionary_in_csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the string : ");
            string message = Console.ReadLine();
            Dictionary<char, int> dict = new Dictionary<char, int>();
            foreach (char ch in message.Replace(" ", string.Empty))
            {
                if (dict.ContainsKey(ch))
                {
                    dict[ch] = dict[ch] + 1;
                }
                else
                {
                    dict.Add(ch, 1);
                }
            }
            foreach (var item in dict.Keys)
            {
                Console.WriteLine(item + " : " + dict[item]);
            }
            Console.ReadKey();
        }
    }
}

How to find Occurrences of a character in a String C#

In this article, we will see How to find Occurrences of a character in a String C# ?  There are multiple ways to find the Occurrences of a ...

Tuesday, 2 February 2021

SQL Performance Tuning and Query Optimization using Execution plan

Sunday, 24 May 2020

In this article we will see what is the Difference between Ref and Out keywords in C#

Here is the simple example to show you the difference between Ref and Out in C#


class Program
    {
        static void Main(string[] args) //calling method
        {
            int valref = 0; //must be initialized
            int valout; //not compulsary
            method1(ref valref);
            Console.WriteLine(valref);
            method2(out valout);
            Console.WriteLine(valout);
            Console.ReadLine();
        }
        static void method1(ref int value) //called method
        {
            value = 1; //not compulsary to initialize 
        }
        static void method2(out int value) //called method
        {
            value = 2; //must be initialized
        }
    }



Below are some differences:

Ref:

  • Must initialized before passing to its called method
  • This is bi-directional
  • The value of the ref parameter can be changed before passing to its calling method.


Out: 

  • Not compulsory to initialize before passing to the method
  • This is uni-directional
  • The value initialized for the Out parameter in the called method will be returned to its calling method.

Difference between Ref and Out keywords in C#

In this article we will see what is the Difference between Ref and Out keywords in C# Here is the simple example to show you the differen...
Here lets discuss what is the Difference between Static Constructor & Non-static Constructors with example


Constructor is usually used to initialize data. However Static Constructor is used to initialize only static members.

Non-static constructors can also be called as Instance Constructors as they need instance to get executed.

Static Constructor
1. Static constructor is used to initialize static data members.
2. Static constructor can't access anything but static members.
3. Static constructor can't have parameters
4. Static constructor can't have access modifiers like Public, Private or Protected.


Lets see the difference between Static Class and Non-static Class

1. Static Class cannot be instantiated unlike the non-static class. You should directly access its Method via the StaticClassName.MethodName

2. Your Program can't tell when it is going to load static class but its definitely loaded before the call.

3. One more point, A Static class will always have the static constructor and its called only once since after that its in the memory for its lifetime.

4. A Static class can contain only static members. So all the members and functions have to be static.

5. A Static class is always sealed since it cannot be inherited further. Further they cannot inherit form any other class (except Object)


• How to declare the static constructor?
Static constructors are declared using a static modifier explicitly while all other remaining constructors are non-static constructors. Non-static constructors can also be called as Instance Constructors as they need instance to get executed.

• How it gets called? 
Static constructors are always called implicitly but the non-static constructors are called explicitly i.e by creating the instance of the class.

• How it gets Executed?
Static constructor executes as soon as the execution of a class starts and it is the first block of code which runs under a class. But the non-static constructors executes only after the creation of the instance of the class. Each and every time the instance of the class is created, it will call the non-static constructor.

Lets summarize this again - 
A static constructor will always execute once in the entire life cycle of a class. But a non-static constructor can execute when an instance of that class is created.
Sample code as below for your reference that shows the declaration, execution of the static and non-static constuctor.

    class TestClass

    {

        static TestClass()

        {

            Console.WriteLine("this is static constructor...");

        }

        public TestClass()

        {

            Console.WriteLine("this is public constructor..");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            TestClass obj = new TestClass();

            TestClass obj1 = new TestClass();

            Console.ReadLine();

        }

    }

Difference between Static Constructor & Non-static Constructors with example

Here lets discuss what is the Difference between Static Constructor & Non-static Constructors with example Constructor is usually u...

Saturday, 28 March 2020

Private Constructor in C#
In this article, you will see the details about Private Constructor.

Private Constructor is a special instance constructor, and it is useful in classes that contain only static members. 
If a class contains one or more private constructors and no public constructors, then the other classes are not allowed to create an instance for that particular class except nested classes.

Private Constructor in C#

Private Constructor in C# In this article, you will see the details about Private Constructor. Private Constructor is a special instance con...

Tuesday, 10 March 2020



The code snippet is given as below.


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

namespace CustomExceptionInCsharp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int res = checkAGE();
                Console.WriteLine(res);
                Console.ReadLine();
            }
            catch (CustomEx_AGE ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static int checkAGE()
        {
            Console.WriteLine("Enter AGE:");
            int age = Convert.ToInt32(Console.ReadLine());
            if (age<18 18="" :="" above="" age="" base="" be="" class="" customex_age="" exception="" msg="" new="" pre="" public="" return="" should="" string="" throw="">

How to create Custom Exception in C# ?

The code snippet is given as below. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System...

Tuesday, 3 March 2020



Using throw statement-


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

namespace throw_throwex
{
    class Program
    {
        static void Main(string[] args)
        {
            //difference between throw and throw ex in c#
            try
            {
                int x = calc(5, 0);
                Console.WriteLine(x);
                Console.ReadLine();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static int calc(int a, int b)
        {
            return a / b;
        }
    }
}
Output:


Using throw ex statement-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace throw_throwex
{
    class Program
    {
        static void Main(string[] args)
        {
            //difference between throw and throw ex in c#
            try
            {
                int x = calc(5, 0);
                Console.WriteLine(x);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static int calc(int a, int b)
        {
            return a / b;
        }
    }
}

Difference between throw and throw ex in c# with simple example

Using throw statement- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.T...

 

Technical Talks © 2015 - Designed by Templateism.com, Distributed By Blogger Templates