Tag: if语句

如果是C#中的/ else语句

为什么这样做? private void button1_Click(object sender, EventArgs e) { if (!checkBox1.Checked) { MessageBox.Show(“The box is not checked!”); } if (checkBox1.Checked == true) { if (label1.BackColor == Color.Red) { label1.BackColor = Color.Blue; } else { label1.BackColor = Color.Red; } } } 但这不是吗? private void button1_Click(object sender, EventArgs e) { if (!checkBox1.Checked) { MessageBox.Show(“The box is not checked!”); […]

使用带有多个if语句的else C#

有没有办法快速检查以下逻辑? 我正在使用C#。 if(a) { } if(b) { } if(c) { } else none of the above //…? execute if all above conditions are false { } 这与使用if-else不同,因为a,b和c都可以同时为真。 所以我不能那样堆叠它们。 我想检查其他的a,b和c都是假的而不写if(!a && !b && !c) 。 这是因为当if条件变得更复杂时,代码会变得非常混乱。 它需要重写大量代码。 这可能吗?

如果在第一次“返回”之后有一个“else”,那么性能是否重要?

我现在已经看到了两种不同的方法来制作布尔返回方法: bool Case1() { if (A) return true; else return false; } bool Case2() { if (A) return true; return false; } 哪一个更快? 为了节省一条线路,使其更清晰,或者是否可以忽略不计的性能增益,是否有理由不写else ?

在IL中看起来像什么?

if语句在编译成IL时会是什么样子? 这是C#中一个非常简单的构造。 sombody可以给我一个更抽象的定义,它究竟是什么?

干(不要重复自己)和如果分配

我想我忘记了一些明显的东西但是我似乎无法找到一种方法来分配一个值,如果它确认一个条件尽可能保持DRY …一些代码来解释我的意思… a = (b > 1) ? b : c; 甚至a = (a > 1) ? a : b; a = (a > 1) ? a : b; 所以当然这里没什么大不了的,但如果要用方法调用替换,(可能是那里的收益率返回)或其他什么,那么我就要调用它两次…… 我只看到将它存放在一个变量中,然后就像上面的代码一样…… 有什么好主意吗? 编辑以便更好地理解:假设我在xml文件中搜索一个值,使用空检查(?。?[])等等 string store_no = myXmlNode.SelectSingleNode(“aChildNode”)?.SelectSingleNode(“myNode”)?.Attributes?[“store_no]?.Value; 所以在这里我将它存储在一个变量中,以便我可以稍后测试它的值。 如果我想检查一个特定的store_no!我将不得不做类似的事情 store_no = (store_no==”STORE23”)? store_no : “unknown”; …是的,不确定这个例子是否足够明确,但这个想法就在这里; 我可能不想将数据存储在变量(例如巨大的数据块)中是否有办法获得相同的结果?

错误地检查响应c#

我有一个代码从POST请求获取响应: //Запрос авторизации бухаря WebRequest request = WebRequest.Create(“http://clannr.org/scripts/login/auth.php”); request.Method = “POST”; string postData = “user=” + Login.Text + “&password=” + Password.Text + “&version=13”; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = “application/x-www-form-urlencoded”; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string […]

在if语句中哪个条件为真

说我有if语句 if(condition1 || condition2 || condition3) { //do something } 当我们进入循环时,是否有可能找出3个条件中的哪一个是真的?

简化使用if的多个if语句(string.contains())

我正在开发一个个人助理程序,我有一个名为input_parse()的方法,它查看输入字符串并检查与伪代码看起来像这样的“命令”对应的单词。 if (input.contains(“argA”) {//execute command A} if (input.contains(“argB”) && input.contains(“argC”)) {//execute command B} 是否有一种更简单的方法来解决这个问题,可能是使用字符串数组的命令,然后根据参数的索引使用switch语句?

C#中的简写条件类似于”’关键字中的SQL’

在C#中有一种写简单的简写方法: public static bool IsAllowed(int userID) { return (userID == Personnel.JohnDoe || userID == Personnel.JaneDoe …); } 喜欢: public static bool IsAllowed(int userID) { return (userID in Personnel.JohnDoe, Personnel.JaneDoe …); } 我知道我也可以使用switch,但是我可能要编写大约50个这样的函数(将一个经典的ASP站点移植到ASP.NET),所以我想让它们尽可能短。

只使用’if-else’语句的’else’部分是否可以接受?

有时,我觉得检查所有条件是否都是真的更容易,但是只处理“其他”情况。 我想我有时会觉得更容易知道某些东西是有效的,并假设所有其他情况都无效。 例如,假设我们只在出现问题时才真正关心: object value = GetValueFromSomeAPIOrOtherMethod(); if((value != null) && (!string.IsNullOrEmpty(value.Prop)) && (possibleValues.Contains(value.prop))) { // All the conditions passed, but we don’t actually do anything } else { // Do my stuff here, like error handling } 或者我应该改变它: object value = GetValueFromSomeAPIOrOtherMethod(); if((value == null) || (string.IsNullOrEmpty(value.Prop)) || (!possibleValues.Contains(value.prop))) { // Do my stuff […]