How to get length of string without using any existing function in C#

Question: Write a program which return a length of string without using any existing function in C#.

Answer:

using System;

namespace consoleProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "my name is pramod kumar thakur";
            // str.Length return length of str.
            Console.WriteLine(str.Length);
            int count = 0;
            foreach (var item in str)
            {
                count++;
            }
            // count and str.Length are equal.
            Console.WriteLine(count);
            Console.ReadLine();
        }
    }
}

Output

Happy Coding.

Tags: , ,