gpt4 book ai didi

c# - VB 的 "Dim"语句在 C# 中的等价物是什么?

转载 作者:可可西里 更新时间:2023-11-01 07:49:36 25 4
gpt4 key购买 nike

拿起 C#,除了示例之外,似乎找不到任何有用的引用。

那么,C# 中的 Dim 是什么?

最佳答案

在 VB 中,Dim 声明了一个特定类型的变量(或者变量类型,如果你没有指定的话)。如果您 Dim x as Foo,这将声明一个名为 xFoo 类型的变量。

在 C# 中,等同于声明类型后跟变量名,如下所示:

Foo x;
int i;

您也可以在同一步骤中分配:

Foo x = new Foo();
int i = 6;

C#支持类型推断,所以你也可以这样做:

// Compiler infers type of x and i based on this assignment.
var x = new Foo(); // x is now of type Foo
var i = 10; // i is now of type int

在 VB 中没有相应类型的 Dim 类似于在 C# 中将类型声明为 Object:

object o = ...; // An object instance can hold any value or reference type.

关于c# - VB 的 "Dim"语句在 C# 中的等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2127738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com