C#编码风格问题 [英] C# coding style question

查看:62
本文介绍了C#编码风格问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循的c#代码样式指南建议类变量(字段)

用驼峰套编码,如下所示:


int recordId;

字符串名称;


它还表明方法和方法参数中的变量使用

驼峰套管,如下所示:


void SetName(int id,string newName)

{

recordId = id;

name = newName;

返回;

}


这一切都很好,花花公子,但我的一些方法有点

难以阅读。很难区分类字段(

具有类范围的范围)和方法变量(具有方法范围的

范围)。我已经开始使用这个了。带有类变量的关键字,

示例:


void SetName(int recordId,string name)

{

this.recordId = recordId;

this.name = name;

}


但是当你这么做的时候,你开始得到很长的代码行。这是我的代码的实际行显示了这个问题:


this.currencyManager =(CurrencyManager)

this .grdPerson.BindingContext [this.dataView];


我很想知道其他c#开发人员做了什么。


谢谢!


Patrick

解决方案

公共类MyClass

{


//成员

private int m_count = 0;

私有字符串m_name = null;


//属性

public int年龄

{

get {return m_count;}

set {m_count = value ;}

}

//方法

公共字符串MyMethod(int id)

{

string tempName = null;

tempName = somehtin .......


返回tempName;

}

}

" Patrick" <是ne ******* @ devzoo.com>在消息新闻中写道:Ox ************* @ TK2MSFTNGP10.phx.gbl ...

我遵循的c#代码样式指南提示类变量(字段用骆驼套管编码,如下:

int recordId;
字符串名称;

它还建议方法和方法参数中的变量使用
驼峰套管,如下:

void SetName(int id,string newName)
{
recordId = id;
name = newName;
返回;
}
这一切都很好,但我的一些方法有点难以阅读。很难区分类字段(具有类范围范围)和方法变量(具有方法范围的范围)。我已经开始使用这个了。关键字与类变量,
例如:

void SetName(int recordId,string name)
{
this.recordId = recordId;
this.name = name;
}

但是,当你这样做时,你会开始获得很长的代码。这是我的代码中显示此问题的真实行:

this.currencyManager =(CurrencyManager)
this.grdPerson.BindingContext [this.dataView];

我很想知道其他c#开发人员做了什么。

谢谢!

Patrick



Patrick写道:

[...]
我已经开始使用this了关键字与类变量,
[...]




只是好奇为什么你觉得有必要做上述事情?可能是

,因为你的方法太长了吗?


就个人而言,我认为不需要任何这样的东西,除了案例

其中参数名称与成员变量的名称相同。


还有一件事......

我使用p_paramName来区分参数和其他方法字段:


//方法

公共字符串MyMethod(int p_id)

{

string tempName = null;

tempName = somehtin .......


return tempName;

}


" Claus Konrad" < CL *** @ whoknows.it>在消息中写道

新闻:OG **************** @ TK2MSFTNGP11.phx.gbl ...

公共类MyClass

{


//成员

private int m_count = 0;

private string m_name = null;


//属性

public int年龄

{

get {return m_count; }

设置{m_count = value;}

}

//方法

公共字符串MyMethod(int id )

{

string tempName = null;

tempName = somehtin .......


返回tempName;

}

}

" Patrick" <是ne ******* @ devzoo.com>在消息中写道

新闻:Ox ************* @ TK2MSFTNGP10.phx.gbl ...

c#代码风格指南我遵循建议类变量
(字段)用骆驼套管编码,如下所示:

int recordId;
字符串名称;

它也建议方法和方法参数中的变量使用骆驼套管,如下所示:

void SetName(int id,string newName)
{
recordId = id;
name = newName;
返回;
}
这一切都很好,但我的一些方法有点难以阅读。很难区分类字段(具有类范围范围)和方法变量(具有方法范围的范围)。我已经开始使用这个了。关键字与类变量,
例如:

void SetName(int recordId,string name)
{
this.recordId = recordId;
this.name = name;
}

但是,当你这样做时,你会开始获得很长的代码。这里是我的代码的
a实线显示了这个问题:

this.currencyManager =(CurrencyManager)
this.grdPerson.BindingContext [this.dataView];

我很想知道其他c#开发人员做了什么。

谢谢!

Patrick



The c# code style guide that I follow suggests that class variables (fields)
be coded with camel casing, like this:

int recordId;
string name;

It also suggests that variables within methods and method parameters use
camel casing, like this:

void SetName(int id, string newName)
{
recordId = id;
name = newName;
return;
}

This is all fine and dandy, but some of my methods are getting a bit
difficult to read. It''s hard to differentiate between class fields (that
have a class-wide scope) and method variables (that have a method-wide
scope). I''ve started to use the "this" keyword with class variables,
example:

void SetName(int recordId, string name)
{
this.recordId = recordId;
this.name = name;
}

BUT, when you do that, you start to get really long lines of code. Here''s a
real line of my code that exhibits this problem:

this.currencyManager = (CurrencyManager)
this.grdPerson.BindingContext[this.dataView];

I''d love to know what other c# developers do.

Thanks!

Patrick

解决方案

public class MyClass
{

//members
private int m_count = 0;
private string m_name = null;

//properties
public int Age
{
get{return m_count;}
set{m_count = value;}
}
//methods
public string MyMethod(int id)
{
string tempName = null;
tempName = somehtin.......

return tempName;
}
}
"Patrick" <ne*******@devzoo.com> wrote in message news:Ox*************@TK2MSFTNGP10.phx.gbl...

The c# code style guide that I follow suggests that class variables (fields)
be coded with camel casing, like this:

int recordId;
string name;

It also suggests that variables within methods and method parameters use
camel casing, like this:

void SetName(int id, string newName)
{
recordId = id;
name = newName;
return;
}

This is all fine and dandy, but some of my methods are getting a bit
difficult to read. It''s hard to differentiate between class fields (that
have a class-wide scope) and method variables (that have a method-wide
scope). I''ve started to use the "this" keyword with class variables,
example:

void SetName(int recordId, string name)
{
this.recordId = recordId;
this.name = name;
}

BUT, when you do that, you start to get really long lines of code. Here''s a
real line of my code that exhibits this problem:

this.currencyManager = (CurrencyManager)
this.grdPerson.BindingContext[this.dataView];

I''d love to know what other c# developers do.

Thanks!

Patrick



Patrick wrote:

[...]
I''ve started to use the "this" keyword with class variables,
[...]



Just curious about why you feel the need to do the above? Could it be
because your methods are too long?

Personally, I don''t see a need for any such thing, except in the case
where the parameter name is the same as that of the member variable.


One more thing...
I use p_paramName to distinguish between parameters and other method fields:

//methods
public string MyMethod(int p_id)
{
string tempName = null;
tempName = somehtin.......

return tempName;
}

"Claus Konrad" <cl***@whoknows.it> wrote in message
news:OG****************@TK2MSFTNGP11.phx.gbl...
public class MyClass
{

//members
private int m_count = 0;
private string m_name = null;

//properties
public int Age
{
get{return m_count;}
set{m_count = value;}
}
//methods
public string MyMethod(int id)
{
string tempName = null;
tempName = somehtin.......

return tempName;
}
}
"Patrick" <ne*******@devzoo.com> wrote in message
news:Ox*************@TK2MSFTNGP10.phx.gbl...

The c# code style guide that I follow suggests that class variables (fields) be coded with camel casing, like this:

int recordId;
string name;

It also suggests that variables within methods and method parameters use
camel casing, like this:

void SetName(int id, string newName)
{
recordId = id;
name = newName;
return;
}

This is all fine and dandy, but some of my methods are getting a bit
difficult to read. It''s hard to differentiate between class fields (that
have a class-wide scope) and method variables (that have a method-wide
scope). I''ve started to use the "this" keyword with class variables,
example:

void SetName(int recordId, string name)
{
this.recordId = recordId;
this.name = name;
}

BUT, when you do that, you start to get really long lines of code. Here''s a real line of my code that exhibits this problem:

this.currencyManager = (CurrencyManager)
this.grdPerson.BindingContext[this.dataView];

I''d love to know what other c# developers do.

Thanks!

Patrick



这篇关于C#编码风格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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