Difference between this and base [closed]

2019-01-25 06:26发布

I am interested to know the difference between this and base object in C#. What is the best practice when using them?

标签: c# this
8条回答
来,给爷笑一个
2楼-- · 2019-01-25 07:20

The two keywords are very different.

  • this refers to the current instance (not the “current class”). It can only be used in non-static methods (because in a static method there is no current instance). Calling a method on this will call the method in the same way as it would if you called it on a variable containing the same instance.

  • base is a keyword that allows inherited method call, i.e. it calls the specified method from the base type. It too can only be used in a non-static method. It is usually used in a virtual method override, but actually can be used to call any method in the base type. It is very different from normal method invocation because it circumvents the normal virtual-method dispatch: it calls the base method directly even if it is virtual.

查看更多
闹够了就滚
3楼-- · 2019-01-25 07:20

"this" keyword points to the address of current object. we can use "this" keyword to represent current object (of current class).

Where as "base" keyword represent to "Parent class"
So if you want to use/call function of parent class you can use "base" Keyword.
base is very useful in function overriding to call function of parent class.

查看更多
登录 后发表回答