C#编码指南:使用“this”。在引用对象中的成员字段/属性时是否? [英] C# coding guidelines: use "this." or not when referring to member fields/properties within the object?

查看:61
本文介绍了C#编码指南:使用“this”。在引用对象中的成员字段/属性时是否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我永远不确定是否使用这个。在引用字段

或同一类中的属性时是否。它显然没有它可以正常工作

但有时我想知道是否使用它。一直可以使代码更容易

以便为其他人理解等等。使用this。立即使用

清除,例如,被引用的变量是

相同类的成员,并且不在方法中声明为局部变量。


这不是什么大不了的事情,但是自从我编写C#以来,这一直是我的想法。


另外,虽然我是考虑一下:如果一个类内部通过属性名称或存储

值的私有变量引用

属性?我做过两件事。我想主要的是如果属性确实比仅设置私有变量的值更多,那么类代码

应该使用属性名而不是私有变量。


Microsoft的C#编码指南是否包含这些问题?

我在哪里可以找到最新的C#副本?编码指南?

I''m never quite sure whether to use "this." or not when referring to fields
or properties in the same class. It obviously works just fine without it
but sometimes I wonder if using this. consistently may make the code easier
to understand for someone else, etc. Using "this." makes it immediately
clear, for example, that the variable being referred to is a member of the
same class and is not declared in the method as a local variable.

Not a big deal but this has been on my mind since I''ve been coding C#.

Also, while I''m thinking about it: should a class internally reference
properties by the property name or by the private variable that stores the
value? I''ve done both. I suppose the main thing is if the property does
more than just set the value of the private variable then the class code
should use the property name instead of the private variable.

Does Microsoft''s C# coding guidelines cover either of these questions?
Where can I find the most up-to-date copy of their C# coding guidelines?

推荐答案



我用它。它更容易维护。每次输入都不容易,

但更容易回到以后。

因为我知道我什么时候编写代码...它是一个成员变量。

编辑,维护......有时候你可能会忘记。


Go and google" brad abrams" 命名约定。


...


偶尔,,,当我写vb.net代码时。我使用我。关键词。它看起来好像垃圾一样,但它的心态与此相同。

vb''他们不理解......我只是喜欢 ;它像java和c#一样

这个单词


我不喜欢vb.net ....它对我不太干净。 />

当人们为参数/ arg列表使用相同的名称时我不喜欢...

和成员变量


公共类Emp

private int empid;


public Emp(int empid)//构造函数

this.empid = empid;

我讨厌这个垃圾。我也不喜欢


异常异常=新异常(坏);


当对象和变量之间的唯一区别是这样的。


无论如何......有我的想法。


我使用m_empid ...我不是真的喜欢_empid ..但是他自己的每一个。


一直没有100%正确答案。有一次我给Brad发了电子邮件。

他就像是选择一个并随身携带。


...

戴夫 <无** @ nowhere.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP04.phx.gbl ...

I use this. Its just easier to maintain. Its not easier to type everytime,
but easier to come back to later.
Because I know when I''m authoring the code ... that its a member variable.
Editing,maintaining ...sometimes you might forget.

Go and google "brad abrams" "naming conventions".

...

On occasion,,, when I wrote vb.net code. I use the "Me." keyword. It looks
like crap, but its the same mentality as this.
vb''ers don''t understand it... and I''m just like "its like java and c# with
the this word"

I dislike vb.net.... its just not as clean to me.

I do not like when people use the same names for their parameter/arg list...
and the member variable

public class Emp
private int empid;

public Emp ( int empid )//constructor
this.empid = empid;
I hate that crap. I also dont like

Exception exception = new Exception("bad");

when the only difference between the object and the variable is the case.

Anyways..... there are my thoughts.

I use m_empid ... I dont'' really like the _empid .. but to each his own.

There are no 100% right answers all the time. One time I emailed Brad..and
he was just like "pick one and go with it".

...
"Dave" <no**@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
我'我永远不确定是否使用这个。在引用同一类中的
字段或属性时是否。它显然没有它可以正常工作
但有时我想知道是否使用它。一致地可以使代码
更易于为其他人理解等等。使用this。例如,明确表示所引用的变量是同一类的成员,并且未在方法中声明为局部变量。

这是一件很重要的事情,但是自从我编写C#以来,这一直是我的想法。

而且,当我在思考它时:一个班级应该在内部参考
属性属性名称还是存储
值的私有变量?我做过两件事。我想主要的是如果属性不仅仅设置私有变量的值,那么类代码
应该使用属性名而不是私有变量。
Microsoft的C#编码指南是否包含这些问题?
我在哪里可以找到最新的C#编码指南?
I''m never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it
but sometimes I wonder if using this. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately
clear, for example, that the variable being referred to is a member of the
same class and is not declared in the method as a local variable.

Not a big deal but this has been on my mind since I''ve been coding C#.

Also, while I''m thinking about it: should a class internally reference
properties by the property name or by the private variable that stores the
value? I''ve done both. I suppose the main thing is if the property does
more than just set the value of the private variable then the class code
should use the property name instead of the private variable.

Does Microsoft''s C# coding guidelines cover either of these questions?
Where can I find the most up-to-date copy of their C# coding guidelines?





" Dave" <无** @ nowhere.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP04.phx.gbl ...

"Dave" <no**@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
我'我永远不确定是否使用这个。在引用同一类中的
字段或属性时是否存在。它显然工作得很好
没有它但有时我想知道是否使用它。一致地可以使其他人更容易理解
代码等。使用this。使其立即清除,例如,被引用的变量是同一类的
成员,并且未在方法中声明为本地变量。

没什么大不了的,但是自从我编写C#以来,这一直是我的想法。

另外,当我在思考它时:一个类内部应该引用
属性名称或存储
值的私有变量的属性?我做过两件事。我想主要的是如果属性不仅仅设置私有变量的值,那么类代码
应该使用属性名而不是私有变量。
Microsoft的C#编码指南是否涵盖这两个问题?
我在哪里可以找到最新的C#编码指南?
I''m never quite sure whether to use "this." or not when referring to
fields or properties in the same class. It obviously works just fine
without it but sometimes I wonder if using this. consistently may make the
code easier to understand for someone else, etc. Using "this." makes it
immediately clear, for example, that the variable being referred to is a
member of the same class and is not declared in the method as a local
variable.

Not a big deal but this has been on my mind since I''ve been coding C#.

Also, while I''m thinking about it: should a class internally reference
properties by the property name or by the private variable that stores the
value? I''ve done both. I suppose the main thing is if the property does
more than just set the value of the private variable then the class code
should use the property name instead of the private variable.

Does Microsoft''s C# coding guidelines cover either of these questions?
Where can I find the most up-to-date copy of their C# coding guidelines?




我为成员变量使用了一个前缀,这样我就可以看到我正在引用的

变量是一个成员变量而不是本地变量。


公共类SomeClass

{

private int mMyInt;

public SomeClass(){

mMyInt = 5;

}

public int MyInt {

得到{

返回mMyInt;

}

设置{

mMyInt = value;

}

}

public int Add(int Value)

{

返回mMyInt + Value;

}

}


对于b $ b这"关键字,我在需要时使用使用该属性而不是

该成员。例如,我为非成员变量创建属性(例如

作为存储在基于Web的应用程序的Request属性中的值)。


... 。

public int RecordId {

get {

object o = Request(" id");

if(o == null){

返回0;

}否则{

return(int)o;

}

}

}

....


通过这样做,我不必编写代码来执行检查。哎呀,我不知道
甚至不记得id。关键,就是这个.RecordId。现在,这是

,其中this来为我们。我们使用this当我们访问属性时,

但不是成员。因为m是m。成员的前缀允许我们识别作为成员的变量
,我们不需要这个进一步确定它。对于

本地变量,我们的商店标准要求它们是camelcased而

参数名称是pascalcased。


这是我们的标准对于我们的商店,不是每个人的选择(并且参数

是pascal套装而不是驼峰套装是针对资本化的

.Net框架的公约......)


参见: http://msdn2.microsoft.com/en-US/lib...02(VS.80).aspx


HTH:)


Mythran



I use a prefix for member variables so that I know by sight that the
variable I''m referencing is a member variable and not a local.

public class SomeClass
{
private int mMyInt;
public SomeClass() {
mMyInt = 5;
}
public int MyInt {
get {
return mMyInt;
}
set {
mMyInt = value;
}
}

public int Add(int Value)
{
return mMyInt + Value;
}
}

For the "this" keyword, I use when I "need" to use the property instead of
the member. For example, I create properties for non-member variables (such
as values stored in the Request property in web-based applications).

....
public int RecordId {
get {
object o = Request("id");
if (o == null) {
return 0;
} else {
return (int) o;
}
}
}
....

By doing this, I don''t have to write code to perform the check. Heck, I
don''t even have to remember the "id" key, just this.RecordId. Now, this is
where the "this" comes in for us. We use "this" when we access properties,
but not members. Because the "m" prefix for members allows us to identify
the variable as a member, we don''t need "this" to further identify it. For
local variables, our shop-standards require they be camelcased while
parameter names are pascalcased.

This is our standard for our shop, not everyones choice (and the parameter
being pascal cased instead of camel cased is against the Capitalization
Conventions for the .Net Framework....)

See: http://msdn2.microsoft.com/en-US/lib...02(VS.80).aspx

HTH :)

Mythran


我使用this。到处都清楚。它只是帮助我知道当前班级的什么是
字段,什么不是,特别是当我在公共汽车上盯着纸质打印输出的时候。 :-)


第二个问题是功能之一,正如你所指出的那样。

通常你想直接在这个领域工作,因为它''

你的私人州的一部分。但是,有时您需要该属性,因为您希望它提供额外的功能。这一切都取决于你的设计和你班上发生的事情。

I use "this." everywhere for clarity. It just helps me know what is a
field of the current class and what isn''t, particularly when I''m
staring at paper printouts on the bus. :-)

The second question is one of functionality, as you pointed out.
Usually you want to work directly with the field, since it''s part of
your private state. However, sometimes you want the property, because
you want extra functionality that it provides. It all depends upon your
design and what''s going on in your class.


这篇关于C#编码指南:使用“this”。在引用对象中的成员字段/属性时是否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆