Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In c# what does ':this' mean [duplicate]

Tags:

c#

c#-4.0

I came across this bit of c# at this link

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();
        }
    }
}
like image 608
spiderplant0 Avatar asked Dec 26 '22 22:12

spiderplant0


1 Answers

It calls another constructor of the same class.

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

    public Foo(int num) 
    {

    }
}

Calling new Foo() will invoke Foo(1).

More info: http://www.dotnetperls.com/this-constructor

like image 91
Artless Avatar answered Dec 28 '22 11:12

Artless