Casting Y or N to bool C#

前端 未结 10 2190
故里飘歌
故里飘歌 2021-02-07 05:46

Just for neatness sake I was wondering, whether it\'s possible to cast Y or N to a bool? Something like this;

bool theanswer = Convert.ToBoolean(input);
         


        
10条回答
  •  伪装坚强ぢ
    2021-02-07 06:07

    how about this.

    bool theanswer = input.Equals("Y", StringComparison.OrdinalIgnoreCase);
    

    or yet safer version.

    bool theanswer = "Y".Equals(input, StringComparison.OrdinalIgnoreCase);
    

提交回复
热议问题