C# Extension Methods – How to Check if a String Contains Special Characters

c++charextension-methodsstring

I am working on a school assignment and have to use extension method to find a string contains a special characters like '!', '@', '#', '$', '%', '^', '&', '*'.

namespace ExtensionMethodAssignment
{

        public class CheckID
        {
            public bool IsAllowedID(string id)
            {
                string specialChar = @"!@#$%^&*";
                foreach (var identification in specialChar)
                {
                    if(id.Contains(identification)) return true;
                }
                return false;
            }

        }


        static void Main(string[] args)
        {
            Console.WriteLine("Type your ID : ");
            string id = Console.ReadLine();
            
            if (id.IsAllowedID() == true)
            {
                Console.WriteLine("ID is not allowed. \n !, @, #, $, %, ^, &, and * are not allowed.");
            }
            else
            {
                Console.WriteLine($"{id} is allowed.");
            }
        }

    }
}

I wrote the code above but I don't know how to use it as an extension method.
I guess it seems like just to make a simple empty method name IsAllowedID and then add up on a Main method but I don't know how.

Best Answer

There are 3 issues in your extension method implementation:

  1. Extension methods should be in a non-generic static class.
  2. Extension methods must be themselves static.
  3. The string id parameter should be prefixed with this keyword.

You can see this in the Extension Methods documentation.

Fixed version:

using System;
using System.Linq;

namespace ExtensionMethodAssignment
{
    //-----vvvvvv--------------
    public static class CheckID  // The class is static
    {
        //-----vvvvvv------------------vvvv-----------
        public static bool IsAllowedID(this string id)  // The method is static and the parameter is prefixed with `this`
        {
            string specialChar = @"!@#$%^&*";
            foreach (var identification in specialChar)
            {
                if (id.Contains(identification)) return true;
            }
            return false;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Type your ID : ");
            string id = Console.ReadLine();

            if (id.IsAllowedID() == true)
            {
                Console.WriteLine("ID is not allowed. \n !, @, #, $, %, ^, &, and * are not allowed.");
            }
            else
            {
                Console.WriteLine($"{id} is allowed.");
            }
        }
    }
}

Live demo

Note that you should include using System.Linq; in order to use the method for checking if a string contains a char (the String.Contains method without it supports a string only).

A side note:
IsAllowedID should either be named something like IsNotAllowedID, or reverse the return of true/false because at the moment it returns the opposite of wether the ID is valid.