Why doesn't the class have a static or persistent property and an instance property with the same name?

I had never interrogated this before. I have an input model with several fields, I wanted to present the names of property strings through the input model so that my Grid could use them:

public class SomeGridRow { public string Code { get;set; } public string Description { get;set; } public const string Code = "Code"; } 

Obviously this gives an error:

Type 'SomeGridRow' already contains the definition of 'Code'

Why can't the CLR handle two properties with the same name that, in my opinion, are separated?

 string code = gridRow.Code; // Actual member from instantiated class string codeField = SomeGridRow.Code; // Static/Const 

Now I use only the Fields child class for my inputs, so I can use SomeGridRow.Fields.Code . It is a little dirty, but it works.

+7
c #
source share
4 answers

Since you can also access static (or non-instances in this case) in the same way (within the same class), and this will be a bit confusing, like this:

 public class SomeGridRow { public string Code { get;set; } public const string Code = "Code"; public void MyMethod() { var thing = Code; //what would this reference? } } 

Because this too:

 public class SomeGridRow { public string Code { get;set; } public void MyMethod() { var thing = Code; //what would this reference? } } 

And this:

 public class SomeGridRow { public const string Code = "Code"; public void MyMethod() { var thing = Code; //what would this reference? } } 

are valid ways to access properties, static or not. He does not answer: "Why can not I?" a question, but more than why he didn’t allow it ... that would be too ambiguous IMO.

+7
source share

Perhaps this is possible, but C # developers wanted to avoid the ambiguities that might result from using (abuse?) Language functions.

Such code will be confusing and ambiguous for users (do I need an instance or a call to a static method?) Which one is right?).

+3
source share

In addition to the points already made about the ambiguity, I would say that in this case it is necessary to rename the names.

If two variables / fields having the same name in the same context, ie class, but different values, rather resemble a problem with names.

If they are exactly the same, you do not need 2 fields.

If they are a little different, you should have more exact names.

+1
source share

In some other languages ​​with similar syntax, you can access a static element through an instance. This way you can access both string.Empty and "abc".Empty .

C # does not allow this (although it pretends to be inside a class or derived class, since you can omit the class name for the static member and you can omit this for the instance member), first avoid confusion (I find this more convenient than confusion tbh, but it's just me, I like switching, too, what I know).

By introducing a stricter rule that allows less ambiguity, it would be counterproductive to allow the new looser rule on the back, which would allow more. Think about how much β€œwhy should I use this with property X, but not property Y?” questions that SO would have if it were resolved (we would have to force this so that the X property is understandable, we had in mind an instance member).

0
source share

All Articles