d3js
V2EX  ›  C#

C# 中 this 关键字在 Forms 类中使用的困惑

  •  
  •   d3js · Aug 21, 2023 · 2179 views
    This topic created in 1185 days ago, the information mentioned may be changed or developed.

    官方对 this 的定义是:

    this 关键字指代类的当前实例,还可用作扩展方法的第一个参数的修饰符。


    问题:

    1 、在 Forms 类中,使用 this 的话,窗体可以弹出

    2 、在 Forms 类中,不使用 this ,自己去创建实例的话,窗体无法弹出

    这是为啥呢?

    https://i.imgur.com/9OtxuB3.png

    https://i.imgur.com/84UT9Pb.png

    chaselen
    chaselen
    Aug 21, 2023❤️1
    1
    你那个`new MyForm()`都死循环了吧,不停地调构造函数。。。
    d3js
    d3js
    OP
    Aug 21, 2023
    2
    @chaselen 大佬,那这个 C# 例子中,要想指代 MyForm 类的当前实例,是不是只能用 this ?
    yuhuai
    yuhuai
    Aug 21, 2023❤️1
    3
    @d3js 你 new 了一个新对象哦,this 代表此窗体 MyForm 这个对象,而你 new 出来的是另外一个
    oott123
    oott123
    Aug 21, 2023❤️1
    4
    用 this:
    this.button.Click

    不用 this:
    button.Click
    forgottencoast
    forgottencoast
    Aug 25, 2023
    5
    图片都没了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1154 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 23:28 · PVG 07:28 · LAX 15:28 · JFK 18:28
    ♥ Do have faith in what you're doing.