-1

I have a general question. I have 2 cases (method Test1 and Test2, and I don't know what is the best approach (Performance, ignore case, all aspects):

    public void Test1(object transcodingStatus)
    {
        string _transcodingStatus = transcodingStatus as string;
        if (!string.IsNullOrEmpty(_transcodingStatus))
        {
            if (_transcodingStatus.ToUpper() == AWSTranscoderStatusBindingModel.COMPLETED.ToString())
            {
                //DO LOGIC
            }
        }
    }

    public void Test2(object transcodingStatus)
    {
        if (transcodingStatus != null)
        {
            AWSTranscoderStatusBindingModel enumVale = AWSTranscoderStatusBindingModel.COMPLETED;
            if (Enum.TryParse(transcodingStatus.ToString(), false, out enumVale))
            {
                if (enumVale == AWSTranscoderStatusBindingModel.COMPLETED)
                {
                    //DO LOGIC
                }
            }
        }
    }

Thanks to all helpers :)

Or Assayag
  • 5,662
  • 13
  • 57
  • 93
  • 1
    I'm curious why the parameter is type `object` to begin with. Why is it not `AWSTranscoderStatusBindingModel?` or `string`? – jmcilhinney Oct 29 '17 at 11:35
  • 1
    The second, undoubtedly. A) because first one does case insensitive comparison wrongly. B) because when there is an existing method is almost always better to use it. Performance? I guess you do not have a MEASURED issue here – Adriano Repetti Oct 29 '17 at 11:36
  • If you can avoid parsing enums from string altogether it will even faster. Serializers typically give the abilty to serialize enums as integers. – StuartLC Oct 29 '17 at 11:37

1 Answers1

1

Enum.TryParse is more efficient than other things in this case

ShineOne
  • 26
  • 1