VB 的 CTYPE() 的正确 C# 转换对应项

发布于 2024-09-09 06:59:36 字数 439 浏览 0 评论 0原文

下面是我将其移植到 C# 重写中的 VB 片段。我的问题是作业后的 receipt_date 是多少?它仍然是一个对象还是一个字符串

Dim receipt_date As Object
receipt_date = CType(dr.Item("RECTDT"), String)

这是正确的 C# 对应项吗?

object receipt_date;
receipt_date = dr["RECTDT"].ToString();

这两个执行后,VB 版本的 receipt_date 是否等于 C# 版本?如果不是,我需要做什么才能做到这一点?谢谢

The below is a snippet from VB that I am porting to a C# rewrite. My question is what is receipt_date after the assignment? Is it still an object or is it a string?

Dim receipt_date As Object
receipt_date = CType(dr.Item("RECTDT"), String)

Would this be the correct C# counterpart?

object receipt_date;
receipt_date = dr["RECTDT"].ToString();

After both of these execute would the VB version, receipt_date be equal to the C# version? If not, what do I need to do to make it so? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹊巢 2024-09-16 06:59:36

是的,您最终会得到相同的结果。它在语义上与 VB 版本不同(您显式调用 ToString() 而不是使用 CType,这大致相当于 C# 中的强制转换),但功能相同。它也更安全,因为在数据库中转换 null 值(在运行时为 DBNull)会引发异常。

不过,为了全面起见,实际的 C# 对应内容是这样的:

var receipt_date = (string)dr["RECTDT"];

不过,作为风格的一点,尽量避免隐式类型(即使用 var 关键字而不是说 stringint)用于简单类型。当类型将来可能发生变化(并且并不重要),或者类型很长和/或复杂并且 var 使其更具可读性时,var 非常有用。在这种情况下,我建议:

string receipt_date = (string)dr["RECTDT"];

Yes, you would end up with the same result. It's semantically different from the VB version (you're calling ToString() explicitly instead of using CType, which is loosely equivalent to a cast in C#), but it's functionally identical. It's also safer, since casting a null value in the database (which would be DBNull in the runtime) would throw an exception.

Just for the sake of being comprehensive, though, the actual C# counterpart would be this:

var receipt_date = (string)dr["RECTDT"];

As a point of style, though, try to avoid implicit typing (i.e., using the var keyword instead of saying string or int) for simple types. var is useful when the type might change in the future (and isn't important), or if the type is long and/or complex and var makes it more readable. In this instance, I would suggest:

string receipt_date = (string)dr["RECTDT"];
云归处 2024-09-16 06:59:36

VB 的 CType 关键字或多或少相当于 Convert.ToString,尽管不完全相同。

因此,VB 中的以下内容...

Dim receipt_date As Object 
receipt_date = CType(dr.Item("RECTDT"), String) 

...最好(或最接近)翻译为 C# 中的以下内容。

object receipt_date;
receipt_date = Convert.ToString(dr.Item["RECTDT"]);

顺便说一下,CType(..., String) 被编译为 Microsoft.VisualBasic.CompilerServices.Conversions.ToString

VB's CType keyword is more or less equivalent to Convert.ToString though not exactly the same.

So the following in VB...

Dim receipt_date As Object 
receipt_date = CType(dr.Item("RECTDT"), String) 

...would be best (or most closely) translated to the following in C#.

object receipt_date;
receipt_date = Convert.ToString(dr.Item["RECTDT"]);

By the way CType(..., String) gets compiled into Microsoft.VisualBasic.CompilerServices.Conversions.ToString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文