Category 1

Monday, 27 February 2023

To find the occurrence of a character in a string in C#, you can use the Count method of the String class, which returns the number of occurrences of a specified character in the string. 


Here is the code snippet for your reference:

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

namespace find_occurence_of_characters_in_string
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the string");
            string ste = Console.ReadLine();
            Console.WriteLine("Enter the character to find its occurence:");
            string character = Console.ReadLine();
            char ch = character.ToCharArray()[0];
            int count = ste.Count(o => o == ch);
            Console.WriteLine("Character '{0}' - occurence {1}", ch, count);
            Console.ReadLine();
        }
    }
}


In this example, the Count method is called on the string str, with a lambda expression that checks if each character in the string is equal to the specified character ch. The resulting count is stored in the count variable, which is then displayed using the Console.WriteLine method.



How to find the occurrence of a character in a string in C# | TechnicalTalks

To find the occurrence of a character in a string in C#, you can use the Count method of the String class, which returns the number of occur...

Friday, 10 February 2023

Abstraction in C#: An Overview of Key Concepts

Thursday, 9 February 2023

What are the OOPs concepts in C#? | Most asked C# Interview Questions

Thursday, 31 March 2022

SOLID stands for: 

  • S - Single-responsibility Principle 
    • The principle defines that A class should have one and only one reason to change, meaning that a class should have only one functionality to handle.

  • O - Open-closed Principle 
    • Objects or entities should be open for extension but closed for modification.

  • L - Liskov Substitution Principle 
    • The principle defines that objects of a superclass shall be replaceable with objects of its subclasses without breaking the application. That requires the objects of your subclasses to behave in the same way as the objects of your superclass. 

  • I - Interface Segregation Principle 
    • Clients should not be forced to depend upon interfaces that they do not use.

  • D - Dependency Inversion Principle: Robert C. Martin’s definition of the Dependency Inversion Principle consists of two parts:
    • High-level modules should not depend on low-level modules. Both should depend on abstractions.
    • Abstractions should not depend on details. Details should depend on abstractions.
We will see each principle in detail with an example.

SOLID Principle in C# - The First 5 Principles of Object Oriented Design

SOLID stands for:  S - Single-responsibility Principle  The principle defines that A class should have one and only one reason to change, m...

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...

 

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