Does C# use the -> pointer notation?

前端 未结 4 894
南旧
南旧 2020-12-11 03:02

I am trying to learn C# and I am familiar with the C++ struct pointing notation ->. I was curious if that crossed over into C#.

Example:



        
4条回答
  •  粉色の甜心
    2020-12-11 03:52

    It's interesting that C# decided to use . rather than -> to reference a member of an object referred to by a class reference, since the semantics are closer to those of C's -> operator than its . operator. For example, in C, if one sees the code a=b; a.X=5; one would recognize that it's writing to field X of some struct a, which is a different struct from b. By contrast, if one sees the code a=b; a->X=5; one would recognize that it's writing to field X of the struct pointed to by both a and b. In C#, the behavior of the code in question would depend upon whether the type of b is a class or a struct.

    The addition of generics to C#, however, would have been difficult if C# had used different dereferencing operators for class and struct types, since it's possible for a particular piece of code to dereference an instance of an interface- constrained type without knowing whether the type in question is a struct or a class; it's unclear which operator should be used if structs used a different operator from classes.

提交回复
热议问题