简体   繁体   English

转换VB if语句将字符串返回到C#的最佳方法是什么

[英]What is the best way to convert VB if statement that return a string to C#

I have a VB "if statement" like 我有一个VB“ if语句”,例如

tagAttribute="<%=If(Model.booleanProperty, "true", "false" )%>"

Would I have to do something like 我需要做类似的事情吗

if(Model.booleanProperty)
  {string booleanProperty = "true"};
else
  { string booleanProperty = "false"};

tagAttribute = booleanProperty;

or is there a nicer way to do it in C#? 还是在C#中有更好的方法呢? I am also using Razor if that makes a difference. 如果这有所作为,我也使用Razor。

Use inline syntax for C# 对C#使用内联语法

var myString = Model.booleanProperty ? "true" : "false";

to write this for Razor, remember to put "@" before the C# expression, so the Razor engine can evaluate it 为Razor编写代码,请记住在C#表达式前加上“ @”,以便Razor引擎可以对其进行评估

tagAttribute='@(Model.booleanProperty ? "true" : "false" )'

您可以这样做:

<%= Model.booleanProperty ? "true" : "false" %>

您可以简单地写:

<%= Model.booleanProperty.ToString().ToLower() %>

Model.booleanProperty ? "true" : "false"

Also, you might find this useful for things like this in future - I wouldn't trust it with converting LINQ though. 另外,将来您可能会发现这对于类似的事情很有用-但是我不相信通过转换LINQ来实现。

http://www.developerfusion.com/tools/convert/csharp-to-vb/ http://www.developerfusion.com/tools/convert/csharp-to-vb/

Either use 无论使用

tagAttribute = Model.booleanProperty.ToString()

or 要么

tagAttribute = Model.booleanProperty ? "true" : "false";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM