Simple functions class I wrote this code a while back and I have recently been rewriting most of my code. I just to know if these methods can be improved. The ones that pop out to me are listed below; others don't matter as much.

Methods that need improving:


FilterInjectionChars
IsValidAlphaNumeric
FilterFigure




namespace Apple.Application.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    class AppleFunctions
    {
        private readonly HashSet&lt;char&gt; _allowedCharacters;

        public AppleFunctions()
        {
            _allowedCharacters = new HashSet&lt;char&gt;(new[] { '-', '.', ' ', 'Ã', '©', '¡', '­', 'º', '³', 'Ã', '‰', '_' });
        }

        public bool GetBoolFromEnum(string EnumValue)
        {
            return Convert.ToBoolean(EnumValue);
        }

        public bool GetBoolFromEnum(int EnumValue)
        {
            return Convert.ToBoolean(EnumValue);
        }

        public int GetEnumFromBool(bool BoolValue)
        {
            return BoolValue ? 1 : 2;
        }

        public bool IsNumber(string StringValue)
        {
            int Number;
            return int.TryParse(StringValue, out Number);
        }

        public int CompareMilleseconds(DateTime time, DateTime from)
        {
            DateTime DateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return Convert.ToInt32((from.Subtract(DateTime).TotalMilliseconds - time.Subtract(DateTime).TotalMilliseconds));
        }

        public int GetUnixTimestamp()
        {
            return (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
        }

        public DateTime GetUnixToDate(int unixTimeStamp)
        {
            DateTime Datetime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            return Datetime.AddSeconds(unixTimeStamp).ToLocalTime();
        }

        public DateTime GetUnixToDate(double unixTimeStamp)
        {
            DateTime Datetime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            return Datetime.AddSeconds(unixTimeStamp).ToLocalTime();
        }

        public int GetDateToUnix(DateTime time)
        {
            DateTime Datetime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            return Convert.ToInt32((time - Datetime).TotalSeconds);
        }

        public string GetDateJoinString(long timeStamp) // MOVE THIS TO GROUP
        {
            string[] time = GetUnixToDate(timeStamp).ToString("MMMM/dd/yyyy").Split('/');
            return String.Format("{0} {1}, {2}", time[0].Substring(0, 3), time[1], time[2]);
        }

        public long GetNow()
        {
            double totalMilliseconds = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
            return ((long)totalMilliseconds);
        }

        private bool IsValidCharacter(char c)
        {
            return char.IsLetterOrDigit(c) || _allowedCharacters.Contains(c);
        }

        /* METHODS TO IMPROOVE ARE BELOW */

        public string FilterFigure(string FigureCode)
        {
            return FigureCode.Any(character =&gt; !IsValidCharacter(character)) ? "lg-3023-1335.hr-828-45.sh-295-1332.hd-180-4.ea-3168-89.ca-1813-62.ch-235-1332" : FigureCode;
        }

        public bool IsValidAlphaNumeric(string inputStr)
        {
            inputStr = inputStr.ToLower();
            if (string.IsNullOrEmpty(inputStr))
                return false;
            return inputStr.All(IsValidCharacter);
        }

        public string FilterInjectionChars(string input)
        {
            input = input.Replace('\u0001', ' ');
            input = input.Replace('\u0002', ' ');
            input = input.Replace('\u0003', ' ');
            input = input.Replace('\t', ' ');
            return input;
        }
    }
}
There are methods that scream louder for improvement that the three you mentioned.

For example these:


public bool GetBoolFromEnum(string EnumValue)
{
  return Convert.ToBoolean(EnumValue);
}

public bool GetBoolFromEnum(int EnumValue)
{
  return Convert.ToBoolean(EnumValue);
}

public int GetEnumFromBool(bool BoolValue)
{
  return BoolValue ? 1 : 2;
}



You are converting some enum to bool or int and back but you are not using this enum here, why?

I guess the enum look probably like this (or rather should):

enum State
{
    Disabled,
    Enabled,        
};


If you used the enum instead of just calling it like that, your methods might have be implemented as:

public bool ToState(State state)
{
    return Convert.ToBoolean(state);
}

public bool ToState(int state)
{
    return ToBoolean((State)state);
}

public int ToState(bool state)
{
    return Convert.ToInt32(state));
}


I find it very weird that you use 1 &amp; 2 as boolean values instead of 1 &amp; 0 that can be much easier converted to int/bool/enum.