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

Tuesday, 25 February 2020

Abstract factory design Pattern
An interface for creating families of related or dependent objects without specifying their concrete classes.

When we need to create a set of related objects or dependent objects which should be used together as families of objects then we use Abstract factory design pattern.

Lets see the difference between Abstract Factory Design Pattern and Factory Design Pattern in c# with example.

Factory pattern
  • This creates object through inheritance.
  • It produces only one product.
  • This implements code in the abstract creator that makes use of the concrete type that sub class produces.
Abstract factory design pattern
  • Creates object through composition.
  • Produce families of products.
  • Concrete factories implements factory method to create product.

Abstract factory design pattern vs Factory pattern c# with example

Abstract factory design Pattern An interface for creating families of related or dependent objects without specifying their concrete class...

Saturday, 15 February 2020




This video is about Factory Design Pattern in C# with example. Here you will learn what is design pattern, what is factory design pattern - its Creational design Pattern, I will show you simple example without its implementation that means traditional approach and then I will show you implementing the same code using Factory design pattern.

Traditional way Code implementation without Factory Design Pattern:

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

namespace WithoutFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            Igym obj = null;
            Console.WriteLine("Which Membership you want?");
            string type = Console.ReadLine();
            if (type == "monthly")
            {
                obj = new Monthly();
            }
            else if (type == "yearly")
            {
                obj = new Yearly();
            }
            Console.WriteLine(obj.getcharges());
            Console.ReadLine();
        }
    }
    public interface Igym
    {
        int getcharges();
    }
    public class Monthly : Igym
    {
        public int getcharges()
        {
            return 2000;
        }
    }
    public class Yearly : Igym
    {
        public int getcharges()
        {
            return 8000;
        }
    }  
}


Code implementation with Factory Design Pattern:

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

namespace WithFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            MembershipFactory obj = new ConcreteMembership();
            Console.WriteLine("which membership charges you want?");
            string type = Console.ReadLine();
            Igym instance = obj.GetGymMembershipType(type);
            Console.WriteLine(instance.getcharges());
            Console.ReadLine();
        }
    }
    public interface Igym
    {
        int getcharges();
    }
    public class Monthly : Igym
    {
        public int getcharges()
        {
            return 2000;
        }
    }
    public class Yearly : Igym
    {
        public int getcharges()
        {
            return 8000;
        }
    }

    public abstract class MembershipFactory //Factory class - Creator
    {
        public abstract Igym GetGymMembershipType(string type);
    }
    public class ConcreteMembership : MembershipFactory //Concrete Creator
    {
        public override Igym GetGymMembershipType(string type)
        {
            switch (type)
            {
                case "monthly":
                    return new Monthly();
                case "yearly":
                    return new Yearly();
                default:
                    throw new ApplicationException(string.Format("membership'{0}' not available", type));
            }
        }
    }
}

Here you saw the difference in traditional way of code implementation and implementing factory design pattern- Factory Design Pattern in C# with example

Factory Design Pattern in C# with example

This video is about Factory Design Pattern in C# with example. Here you will learn what is design pattern, what is factory design pat...

Saturday, 1 February 2020

Welcome to Technical Talks!

Here we are going to see how simply we make an angular app using .Net MVC WebAPI as service and SQL server as a backend.

You will come to know the flow of implementation and live running app.

So in order to get started, lets create a table called 'customers', the script as given below :

Step 1: Create table - customers


CREATE TABLE [customers](
[customer_id] [int] IDENTITY(1,1) NOT NULL,
[first_name] [varchar](255) NOT NULL,
[last_name] [varchar](255) NOT NULL,
[phone] [varchar](25) NULL,
[email] [varchar](255) NOT NULL,
[street] [varchar](255) NULL,
[city] [varchar](50) NULL,
[state] [varchar](25) NULL,
[zip_code] [varchar](5) NULL,
[dob] [date] NULL,
[anniversary] [date] NULL,
PRIMARY KEY CLUSTERED 
(
[customer_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

The table looks as shown below.

Step 2: Create WebAPI Service - ASP.Net MVC WebAPI

In order to build the Restful API and use it in our angular app, here we are going to use ADO.Net Entity Framework.

Let's create the WebAPI project - I name this project as 'API' as shown in below screen. 


Add the ADO.Net Entity Data Model in Models folder of the project and provide the database connection and select the customers table from the list to make use of this table for customer data in API.

Please refer the below screen.


Now lets create a controller called - customers selecting entity framwork type as shown below.
In order to add CORS in our WebAPI, please add the below code in your WebApiConfig.cs file under App_Start folder of the project.

            var corsAttr = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(corsAttr);

so far, we are done with the backend services and now we want to show the data through customers webapi in angular.

Lets move on..

Simple Angular Application - Get started

Welcome to Technical Talks! Here we are going to see how simply we make an angular app using .Net MVC WebAPI as service and SQL server ...

 

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