VB.NET 相当于 C#“As”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2450185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:04:44  来源:igfitidea点击:

VB.NET equivalent of C# "As"

.netvb.net

提问by JoelFan

What is the equivalent in VB.NET of the C# Askeyword, as in the following?

VB.NET 中 C# As关键字的等效项是什么,如下所示?

var x = y as String;
if (x == null) ...

回答by Hans Passant

It is TryCast:

这是TryCast:

Dim x As String = TryCast(y, String)
If x Is Nothing Then ...

回答by Morten Anderson

Trycast is what you're looking for.

Trycast 正是您要找的。

Dim x = TryCast(y, String)

回答by Guffa

TryCast:

试播:

Dim x = TryCast(y, String)
if (x Is Nothing) ...

回答by Alex Essilfie

Here you go:

干得好:

C# code:

C#代码:

var x = y as String;
if (x == null) ...

VB.NET equivalent:

VB.NET 等效项:

Dim x = TryCast(y, String)
If (x Is Nothing) ...

回答by Oskar Kjellin

Dim x = TryCast(y, [String])

Dim x = TryCast(y, [String])

回答by evanboissonnot

You can use it with ?:

您可以将其用于?

TryCast(item, String)?.Substring(10)

It allows you to manage nullable without if:)

它允许您在没有if:) 的情况下管理可空值