What is the meaning of “this” in C#

前端 未结 8 1285
猫巷女王i
猫巷女王i 2020-12-07 01:44

Could anyone please explain the meaning \"this\" in C#?

Such as:

// complex.cs
using System;

public struct Complex 
{
   public int real;
   public          


        
8条回答
  •  盖世英雄少女心
    2020-12-07 02:14

    this is a variable which represents the current instance of a class. For example

    class SampleClass {
    public SampleClass(someclass obj) {
    obj.sample = this;
    }
    }
    

    In this example, this is used to set the "sample" property on someclass obj, to the current instance of SampleClass.

提交回复
热议问题