简体   繁体   English

C#中的引用和对象

[英]References and objects in C#

I am currently writing an assignment for school. 我目前正在为学校写作。 I have trouble on how I should comment different parts of my application. 我在如何评论应用程序的不同部分方面遇到了麻烦。

I need to read from files, and therefore I use the StreamReader class. 我需要从文件中读取,因此我使用StreamReader类。

When I write this 当我写这个

StreamReader reader;

I have trouble defining what I just did. 我无法定义我刚刚做的事情。 The C# Yellow Book by Rob Miles defines this very similar code Rob Miles的C#Yellow Book定义了这个非常相似的代码

Account RobsAccount;

What you actually get when the program obeys that line is the creation of a reference called RobsAccount. 当程序服从该行时,您实际获得的是创建名为RobsAccount的引用。

He follows with an analogy to a luggage tag 他跟着一个行李标签的比喻

You can think of them as a bit like a luggage tag, in that they can be tied to something with a piece of rope. 你可以把它们看作有点像行李标签,因为它们可以用一根绳子系在一起。 If you have the tag you can then follow the rope to the object it is tied to. 如果你有标签,那么你可以沿着绳索到它所绑定的物体上。

When Rob Miles write this later on 当Rob Miles稍后写这篇文章时

RobsAccount = new Account();

He describes it as 他将其描述为

(...)creating an instance of the class and then connecting our tag to it. (...)创建类的实例,然后将我们的标记连接到它。

RobsAccount = new Account();

is similar to my case, where I simply write 类似于我的情况,我只是写

reader = new StreamReader(file, Encoding.Default);

After reading a couple of different books on C#, I still cannot confidently comment what is called what. 在阅读了几本关于C#的不同书籍之后,我仍然无法自信地评论所谓的内容。 They seem to use different names and different analogies. 他们似乎使用不同的名称和不同的类比。

So my question is: 所以我的问题是:

What would be a suitable comment to this 什么是适当的评论

StreamReader reader;

And what would be a suitable comment to this 什么是适当的评论

reader = new StreamReader(file, Encoding.Default);

My programming teacher says that when you write 我的编程老师说你写的时候

StreamReader reader;

You are making a variable of the type StreamReader with the name reader . 您正在使用名称阅读器创建 StreamReader类型的变量。 This variable is actually a pointer (hence, reference) to an object that is created by writing 该变量实际上是通过写入创建的对象的指针(因此,引用)

new StreamReader(file, Encoding.Default);

Is this the right way to describe it? 这是描述它的正确方法吗?

StreamReader reader;

This is variable declaration. 这是变量声明。

reader = new StreamReader(file, Encoding.Default);

Here you are instantiating the variable "reader" with the object of StreamReader class. 在这里,您将使用StreamReader类的对象实例化变量“reader”。

There's a difference between declaring a variable and instantiating a variable in C#. 声明变量和在C#中实例化变量之间存在差异。 When you write the line 当你写行

StreamReader reader;

You are creating a reference to a null StreamReader object with the name reader. 您正在使用名称reader创建对null StreamReader对象的引用。 If you try to use it before instantiating it, you will get a null object reference. 如果在实例化之前尝试使用它,则会得到一个空对象引用。 When you make your comment, you can state that you have declared an object reference. 在进行评论时,您可以声明已声明了对象引用。

When you write the following line 当你写下面这一行

reader = new StreamReader(file, Encoding.Default);

You are instantiating the reader object. 您正在实例化reader对象。 That is, you are giving a value to the object reference you created earlier. 也就是说,您要为之前创建的对象引用赋值。 When you comment on this line, you can say that you have instantiated the object. 当您对此行发表评论时,您可以说您已实例化该对象。 After this point you should not receive a NullReference exception if you attempt to reference this object. 在此之后,如果您尝试引用此对象,则不应收到NullReference异常。

If you are talking about internal documentation, then you should comment on the functionality of variables when you declare them (what is it for--why did you make it). 如果您正在讨论内部文档,那么在声明变量时应该对变量的功能进行评论(它是为什么 - 为什么要这样做)。 Here's a link for some quick tips on documentation: (download) 这里有一些关于文档的快速提示的链接:( 下载)

If your assignment is to describe what is happening behind the scenes (ie to "Play Computer") then 如果你的任务是描述幕后发生的事情(即“玩电脑”)那么

StreamReader reader;

is a declaration. 是宣言。 It means that Java has reserved that name as a reference and it knows that reference will point to a location in memory that has data representing that type ('class'). 这意味着Java已将该名称保留为引用,并且它知道引用将指向内存中具有表示该类型(“类”)的数据的位置。

When you do 当你这样做

new StreamReader(file, Encoding.Default);

You are actually modifying the contents of the container to which the reference points. 您实际上正在修改引用指向的容器的内容。 The 'new' keyword tells Java you want to get some memory and the NewReader() function call modifies that memory while the JVM (virtual machine) maintains the name/reference->container/data relationship. 'new'关键字告诉Java你想获得一些内存,而NewReader()函数调用修改了那个内存,而JVM(虚拟机)维护了name / reference-> container / data关系。

When I write this 当我写这个

 StreamReader reader; 

I have trouble defining what I just did. 我无法定义我刚刚做的事情。 The C# Yellow Book by Rob Miles defines this very similar code Rob Miles的C#Yellow Book定义了这个非常相似的代码

 Account RobsAccount; 
  • The only thing similar is that in both cases, a variable is being declared. 唯一相似的是,在这两种情况下,都会声明一个变量。
  • Forget the analogy, although it's correct, it seems to be confusing you more than it's doing any good, if there's something you enjoy doing, hobby etc I can tailor one for you so that you can relate to it... 忘记这个比喻,虽然这是正确的,但是如果有你喜欢做的事情,爱好等等,那么它似乎会使你感到困惑,我可以为你量身定制一个,这样你就能与之相关......

When Rob Miles write this later on 当Rob Miles稍后写这篇文章时

 RobsAccount = new Account(); 

He describes it as (...)creating an instance of the class and then connecting our tag to it. 他将其描述为(...)创建类的实例,然后将我们的标记连接到它。

So let's break that down... 所以让我们打破这个......

// The code below is instantiating a new object.
// new Account();

// The code below is declaring a variable, at the moment it is null
// This is because it isn't referencing any objects that we've created.
Account RobsAccount;

// The `=` character is an assignment operator, and we'll need it to 
// assign objects that we create to variables.
// The code below will assign an object of type Account, to the variable
// RobsAccount which has a type of Account.
RobsAccount = new Account();

// Now the `RobsAccount` variable is pointing to the object
// that we just instantiated

Moving on to your final question... 继续你的最后一个问题......

What would be a suitable comment to this 什么是适当的评论

 StreamReader reader; 

And what would be a suitable comment to this 什么是适当的评论

 reader = new StreamReader(file, Encoding.Default); 
// A declaration of a StreamReader variable.
StreamReader reader;

// Assigning a the `reader` variable with a new StreamReader object 
// that has been instantiated with the following parameters
// - The file path of the document to read
// - The encoding of the file to read
reader = new StreamReader(file, Encoding.Default);

Keywords: 关键词:

  • Type: Every variable or constant has a Type , it might be a string , an integer , a boolean , StreamReader or a type that you've defined using a class or struct , any expression that evaluates to a value has a Type . 类型:每个变量或常量都有一个Type ,它可能是一个string ,一个integer ,一个booleanStreamReader或您使用classstruct定义的类型,任何求值的表达式都有一个Type
  • Class: A reference type references an object. 类: reference type引用对象。 Imagine your class register, it will reference you, there are many registers, but they all reference you as a person. 想象一下你的班级注册,它会引用你,有很多寄存器,但它们都引用你作为一个人。
  • Struct: A value type that references a value. 结构:引用值的value type If your class register was to reference your name by value, and you were to change your name, the register would still be referencing the old value. 如果您的班级注册是按价值引用您的姓名,并且您要更改您的姓名,则该注册仍将引用旧值。
  • Instantiating: To create an instance of a class. 实例化:创建类的实例。
  • Object: Is what you receive as a result of instantiating a class 对象:是实例化类的结果
  • Variable: Is an instance of a type , if the variable is a value type , then it will always consist of a value, if the variable is a reference type , then it may also be null, as a reference type may not be referencing anything at all, however, a value type will always have default value... (Don't be tricked by int? it's a value type! It just translates into Nullable<int> , but the Nullable type is in fact a struct 变量:是一个type的实例,如果变量是一个value type ,那么它将始终由一个值组成,如果该变量是一个reference type ,那么它也可能是null,因为reference type可能没有引用任何东西但是,一个值类型总是有默认值...(不要被int?欺骗int?它是一个值类型!它只是转换为Nullable<int> ,但Nullable类型实际上是一个struct

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

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