In C # what does ": this" mean

I met this C # bit at this link

I can not understand this line ...

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

It is a bit like inheriting from a base class, but I have never seen this like 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(); } } } 
+4
source share
3 answers

It calls another constructor of the same class.

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

Calling new Foo() calls Foo(1) .

Further information: http://www.dotnetperls.com/this-constructor

+8
source

this(StockTicker.Instance) starts another class constructor:

Using Constructors (C # Programming Guide) :

A constructor can call another constructor in the same using the this . Like base , it can be used with or without parameters, and any parameters in the constructor are available as parameters to this or as part of an expression.

+2
source

It calls another constructor

0
source

Source: https://habr.com/ru/post/1496402/


All Articles