C# Xamarin 退出函数
C# Xamarin Exit function
我有一个 if 语句,我想在其中中止后续步骤并重新开始。但是我怎样才能使用 Exit; (比如 PHP)之类的?
这是我的代码:
if (SelectedQuantity != null | SelectedProduct != null)
{
string toast = string.Format("You may only fill 1 of the input fields!");
Toast.MakeText(this, toast, ToastLength.Long).Show();
}
在 "if" 之后,它将值放入数据库中,但如果您进入 if 语句,我想中止该过程。有人知道怎么做吗?
这取决于上下文,但通常您可以在任何给定点使用 return
退出该方法
您的代码没有真正的表现力(我们不知道它周围是什么)。但我会告诉你使用 return
语句。
如果您的 if 语句直接在方法中,您可以使用 return
关键字提前结束该方法(除非该方法是一个函数并返回一个值,那么您 return someValue
。如果你在一个循环中,你跳过并使用 continue
关键字继续循环,你使用 break
关键字跳出循环。另外,对此我深表歉意,但我建议采取额外的时间首先学习 C#,因为这是非常基础的;更不用说您在 if 语句中的 or 操作没有缩短 ||
而且您 string.format 作为字符串文字更好。
尽管这不是您问题的一部分;我确实看到有人发表评论,而你略带讽刺地忽略了它(尽管他们的评论是讽刺)。辩护是因为这是 C# 基础知识。
无论如何,我将花一点时间来解释它,如果你欣赏这些知识,它可能会激励你深入研究 C# 语言,或多或少地以正确的方式做事。
至于使用string.format
,这应该只用于实际格式化字符串;它擅长的。然而;当你只有一个字符串文字(引号中的字符在技术上是恒定的)时,应该避免 string.format 。这是我为两个微型单行应用程序生成的 IL 代码,仅用于演示 string.format 和在可以完成时使用字符串文字之间的区别。
应用使用字符串文字。
namespace StringFormatVSStringLiteral
{
class Program
{
static void Main(string[] args)
{
var value = "My name is Michael";
}
}
}
IL 生成
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 8 (0x8)
.maxstack 1
.locals init ([0] string 'value')
IL_0000: nop
IL_0001: ldstr "My name is Michael"
IL_0006: stloc.0
IL_0007: ret
} // end of method Program::Main
应用使用 string.Format
namespace StringFormatVSStringLiteral
{
class Program
{
static void Main(string[] args)
{
var value = string.Format("My name is Michael");
}
}
}
IL 生成
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 18 (0x12)
.maxstack 2
.locals init ([0] string 'value')
IL_0000: nop
IL_0001: ldstr "My name is Michael"
IL_0006: call !!0[] [mscorlib]System.Array::Empty<object>()
IL_000b: call string [mscorlib]System.String::Format(string,
object[])
IL_0010: stloc.0
IL_0011: ret
} // end of method Program::Main
所以你可以看到在使用 string.Format 时应该尽可能避免,因为它增加了堆栈和调用。如果你有这个,比如在一个循环中,或者一个资源密集型应用程序,那么差异可能很关键。
至于或运算符;在 C# 中,您可以通过将 ||
加倍来短路或 |
。这样做是测试第一个值;在 or 运算符中,如果第一次测试成功,它会跳过测试第二步;这显然更快。当您将运算符写为 |
时,您会强制它测试每个值,无论第一个值是否完成操作。另请注意,由于其内部工作方式,第二个或运算符 |
在 C# 中用于二元运算和标志。 and 运算符是相同的,其中 &
测试双方,即使第一个失败;可以而且应该使用 &&
将其短路。同样的事情适用于 &
运算符,它用于二元运算以及调整标志。
我有一个 if 语句,我想在其中中止后续步骤并重新开始。但是我怎样才能使用 Exit; (比如 PHP)之类的? 这是我的代码:
if (SelectedQuantity != null | SelectedProduct != null)
{
string toast = string.Format("You may only fill 1 of the input fields!");
Toast.MakeText(this, toast, ToastLength.Long).Show();
}
在 "if" 之后,它将值放入数据库中,但如果您进入 if 语句,我想中止该过程。有人知道怎么做吗?
这取决于上下文,但通常您可以在任何给定点使用 return
退出该方法
您的代码没有真正的表现力(我们不知道它周围是什么)。但我会告诉你使用 return
语句。
如果您的 if 语句直接在方法中,您可以使用 return
关键字提前结束该方法(除非该方法是一个函数并返回一个值,那么您 return someValue
。如果你在一个循环中,你跳过并使用 continue
关键字继续循环,你使用 break
关键字跳出循环。另外,对此我深表歉意,但我建议采取额外的时间首先学习 C#,因为这是非常基础的;更不用说您在 if 语句中的 or 操作没有缩短 ||
而且您 string.format 作为字符串文字更好。
尽管这不是您问题的一部分;我确实看到有人发表评论,而你略带讽刺地忽略了它(尽管他们的评论是讽刺)。辩护是因为这是 C# 基础知识。
无论如何,我将花一点时间来解释它,如果你欣赏这些知识,它可能会激励你深入研究 C# 语言,或多或少地以正确的方式做事。
至于使用string.format
,这应该只用于实际格式化字符串;它擅长的。然而;当你只有一个字符串文字(引号中的字符在技术上是恒定的)时,应该避免 string.format 。这是我为两个微型单行应用程序生成的 IL 代码,仅用于演示 string.format 和在可以完成时使用字符串文字之间的区别。
应用使用字符串文字。
namespace StringFormatVSStringLiteral
{
class Program
{
static void Main(string[] args)
{
var value = "My name is Michael";
}
}
}
IL 生成
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 8 (0x8)
.maxstack 1
.locals init ([0] string 'value')
IL_0000: nop
IL_0001: ldstr "My name is Michael"
IL_0006: stloc.0
IL_0007: ret
} // end of method Program::Main
应用使用 string.Format
namespace StringFormatVSStringLiteral
{
class Program
{
static void Main(string[] args)
{
var value = string.Format("My name is Michael");
}
}
}
IL 生成
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 18 (0x12)
.maxstack 2
.locals init ([0] string 'value')
IL_0000: nop
IL_0001: ldstr "My name is Michael"
IL_0006: call !!0[] [mscorlib]System.Array::Empty<object>()
IL_000b: call string [mscorlib]System.String::Format(string,
object[])
IL_0010: stloc.0
IL_0011: ret
} // end of method Program::Main
所以你可以看到在使用 string.Format 时应该尽可能避免,因为它增加了堆栈和调用。如果你有这个,比如在一个循环中,或者一个资源密集型应用程序,那么差异可能很关键。
至于或运算符;在 C# 中,您可以通过将 ||
加倍来短路或 |
。这样做是测试第一个值;在 or 运算符中,如果第一次测试成功,它会跳过测试第二步;这显然更快。当您将运算符写为 |
时,您会强制它测试每个值,无论第一个值是否完成操作。另请注意,由于其内部工作方式,第二个或运算符 |
在 C# 中用于二元运算和标志。 and 运算符是相同的,其中 &
测试双方,即使第一个失败;可以而且应该使用 &&
将其短路。同样的事情适用于 &
运算符,它用于二元运算以及调整标志。