Skip to main content
deleted 29 characters in body
Source Link
Andras Vass
  • 11.7k
  • 1
  • 40
  • 51

Sometimes it is the least evil.
Sometimes you cannot do much better than thatThere is a fully legit reason to use it in C#:.
Especially in C#, if the code must be extensible from VB.NET as well. (Otherwise, I would not.)

Since VB.NET is is case insensitive, there is no simple way to access the protected field member in this code:

public class CSharpClass { protected int field; public int Field { get { return field; } } } 

E.g. this will access the property getter, not the field:

Public Class VBClass Inherits CSharpClass Function Test() As Integer Return Field End Function End Class 

Heck, I cannot even write field in lowercase - VS 2010 just keeps correcting it.

In order to make it easily accessible to derived classes in VB.NET, one has to come up with another naming convention. Prefixing an underscore is probably the least intrusive and most "historically accepted" of them.

Sometimes it is the least evil.
Sometimes you cannot do much better than that.
Especially in C#, if the code must be extensible from VB.NET as well.

Since VB.NET is is case insensitive, there is no simple way to access the protected field member in this code:

public class CSharpClass { protected int field; public int Field { get { return field; } } } 

E.g. this will access the property getter, not the field:

Public Class VBClass Inherits CSharpClass Function Test() As Integer Return Field End Function End Class 

Heck, I cannot even write field in lowercase - VS 2010 just keeps correcting it.

In order to make it easily accessible to derived classes in VB.NET, one has to come up with another naming convention. Prefixing an underscore is probably the least intrusive and most "historically accepted" of them.

There is a fully legit reason to use it in C#: if the code must be extensible from VB.NET as well. (Otherwise, I would not.)

Since VB.NET is is case insensitive, there is no simple way to access the protected field member in this code:

public class CSharpClass { protected int field; public int Field { get { return field; } } } 

E.g. this will access the property getter, not the field:

Public Class VBClass Inherits CSharpClass Function Test() As Integer Return Field End Function End Class 

Heck, I cannot even write field in lowercase - VS 2010 just keeps correcting it.

In order to make it easily accessible to derived classes in VB.NET, one has to come up with another naming convention. Prefixing an underscore is probably the least intrusive and most "historically accepted" of them.

Source Link
Andras Vass
  • 11.7k
  • 1
  • 40
  • 51

Sometimes it is the least evil.
Sometimes you cannot do much better than that.
Especially in C#, if the code must be extensible from VB.NET as well.

Since VB.NET is is case insensitive, there is no simple way to access the protected field member in this code:

public class CSharpClass { protected int field; public int Field { get { return field; } } } 

E.g. this will access the property getter, not the field:

Public Class VBClass Inherits CSharpClass Function Test() As Integer Return Field End Function End Class 

Heck, I cannot even write field in lowercase - VS 2010 just keeps correcting it.

In order to make it easily accessible to derived classes in VB.NET, one has to come up with another naming convention. Prefixing an underscore is probably the least intrusive and most "historically accepted" of them.