Question Dot notation .Equals

Tomuks69

New member
Joined
Feb 22, 2021
Messages
2
Programming Experience
1-3
I was wondering how does this code of line works:
Sample:
var a = x.Equals(y) returns true

Isnt it like x is being passed as variable something like:

a = EqualsCustom(x, y)
Public bool EqualsCustom = {
//Some comparison code..
}
I am just wondering how this dot notation works other than accessing some members?
 
Yes, if you were looking at this as a old fashioned C programmer. But in modern object oriented eyes, no.
 
Dot notation accesses/calls a member of the type or instance of type. Here 'x' is an instance of a type, and that type defines an Equals method, so the 'x' object can call it.

Equals method is actually defined in type object that is the base type for all types, inherited types can override this method for more specialized comparison.
 
Let say I have a class with a property x=1. When this class is created it also inherits from object base class. This base class contains ToString() method. Why does this ToString() method takes these two properties via dots like x.ToString() not like ToString(x);

like how does it not to take that property into ToString() method
 
Actually, the base ToString() knows nothing about x property.
 
They are not standalone static methods, they belong to the object instance. Even for static methods you would qualify by type, because these members belong to the type, for example int.TryParse(args)
See Classes - C# language specification
 
You may compare it to calling methods or using fields/properties inside classes where you don't qualify the object, but that is also shorthand for qualifying this object, for example this.AMethod() or this.AProperty.
 
Back
Top Bottom