在C#中':this'是什么意思 [英] In c# what does ':this' mean

查看:402
本文介绍了在C#中':this'是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚这行...

I cant figure out this line ...

public StockTickerHub() : this(StockTicker.Instance) { }

它看起来有点像从基类继承,但是我以前从未见过这个这样使用。

It looked a bit like inheriting from a base class but I havent seen this used like this before.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace SignalR.StockTicker
{
    [HubName("stockTickerMini")]
    public class StockTickerHub : Hub
    {
        private readonly StockTicker _stockTicker;

        public StockTickerHub() : this(StockTicker.Instance) { }

        public StockTickerHub(StockTicker stockTicker)
        {
            _stockTicker = stockTicker;
        }

        public IEnumerable<Stock> GetAllStocks()
        {
            return _stockTicker.GetAllStocks();
        }
    }
}


推荐答案

它调用同一类的另一个构造函数。

It calls another constructor of the same class.

public class Foo
{
    public Foo() : this (1) { }

    public Foo(int num) 
    {

    }
}

调用 new Foo()将调用 Foo( 1)

更多信息: http://www.dotnetperls.com/this-constructor

这篇关于在C#中':this'是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆