声明/定义变量的不同方法?

发布于 2024-08-21 11:20:10 字数 557 浏览 10 评论 0原文

这是一个VB.Net新手问题。我对声明和定义变量的不同方式感到困惑。

这是一个示例

Dim URL As String = http://www.c-sharpcorner.com/default.asp
Dim request As HttpWebRequest = WebRequest.Create(URL)
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())

我什么时候应该使用1.什么都不用,2.调用 Create() 方法, 3. 调用该对象除 Create() 之外的另一个方法, 4. 使用 New 词?

This is a VB.Net newbie question. I'm confused at the different ways to declare and define variables.

Here's an example:

Dim URL As String = http://www.c-sharpcorner.com/default.asp
Dim request As HttpWebRequest = WebRequest.Create(URL)
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())

When should I use 1. nothing, 2. call the Create() method, 3. Call another method of the object besides Create(), and 4. use the New word?

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

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

发布评论

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

评论(1

遗忘曾经 2024-08-28 11:20:10

.Net 中的大多数原始类型(Int32、String 等)都有允许您声明新实例的文字语法。如果可用,它可能应该是您的首选。上面的 URL 变量就是一个例子。

您的下一个选择可能是 New 关键字。当您尝试实例化的类型在设计时已知时,这很好。例如,如果您只是尝试实例化实现接口的类型的实例,但不关心返回的对象的具体类型,则这可能不合适。

在这种情况下可以使用的设计模式(设计时类型未知)是 工厂方法。工厂方法的初始化方式会影响它返回的对象的类型。

如果一个类没有外部可见的构造函数,那么可能是因为该类的开发人员希望保留在运行时决定他将返回哪种类型的权利。在这种情况下,他一般会提供一个Factory Method(按照约定以Create关键字为前缀)。该方法不一定位于您尝试实例化的类上,但可能会添加到 API 中的其他某个类中,该类具有首先决定要返回哪个具体类,其次能够提供所需的上下文创建对象所需的依赖项。

总之,您的决策路径可能应该是..

  1. 文字
  2. 构造函数
  3. 工厂方法

有趣的是 - DateTime 数据类型是 VB.Net 具有文字语法而 C# 没有的情况。 1999 年 5 月 31 日可以使用语法 #5/31/1993# 在 VB.Net 中实例化为 DateTime 对象。要在 C# 中实例化相同的日期值,需要使用构造函数 new DateTime(1999, 5, 31)

Most primitive types (Int32, String etc) in .Net have a literal syntax that allows to you declare a new instance. Where this is available, it should probably be your first choice. Your URL variable from above would be an example of this.

Your next choice would probably be the New keyword. This is good where the type that you are trying to instantiate is known at design time. This might not be appropriate for example if you are just trying to instantiate an instance of a type that implements an interface but do not care about the concrete type of the object that is returned.

A design pattern that you can use in this case (the type is not known at design time) is the Factory Method. The way that the Factory Method is initialized, can influence the type of the object that it returns.

If a class does not have an externally visible constructor then it is probably because the developer of that class wanted to reserve the right to decide at runtime which type he will return. In this case, he will generally provide a Factory Method (prefixed with the Create keyword by convention). The method will not necessarily be on the class that you are trying to instantiate but might be added to some other class in the API that has the context required to firstly make the decision as to which concrete class to return and secondly has the ability to provide the necessary dependencies to create the object.

In summary your decision path should probably be..

  1. Literal
  2. Constructor
  3. Factory Method

As an interesting aside - the DateTime data type is a case where VB.Net has a literal syntax and C# does not. The 31st of May 1999 can be instantiated as a DateTime object in VB.Net using the syntax #5/31/1993#. To instantiate the same date value in C# would require the use of the constructor new DateTime(1999, 5, 31).

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