2017-02-13 4 views
-3

Hallo ich versuche, C# zu lernen, ich kam in diesem Code aus einem C# Tutorial:Klasse Instanziierung Funktion

Public Class A() 
{ 
    Public A() : this(capacity:10) 
    { 
    } 

    public int capacity 
    { 
     get { return _buffer.length;} 
    } 
} 

ich einfach nicht verstehen, warum er : zwischen Public A() und this(capacity:10) verwendet.

Ich wusste nicht, wonach man auf Google suchen soll, also entschied ich mich, hier zu fragen.

Meine Frage ist: wofür wird das verwendet und warum?

+0

Persönlich würde ich versuchen, schwieriger ist es, herauszufinden, bevor Sie auf Stack Overflow nachfragen. –

Antwort

0

Der angegebene Code kompiliert nicht. Wahrscheinlich kann der Code so etwas wie diese:

// lower case for "public" and "class" 
    public class A { 
    // _buffer is addressed in "capacity" property, let it be an array 
    private int[] _buffer = new int[10]; 

    // this constructor is mandatory for the code below 
    public A(int capacity) { 
    } 

    // Now, your question: 
    // "this(capacity: 10)" calls the costructor above 
    // "public A(int capacity)" 
    // while passing the "capacity" parameter by its name 
    public A() : this(capacity: 10) 
    { 
    } 

    public int capacity { 
    get { 
     return _buffer.Length; 
    } 
    } 

Wenn ich erratenthis(capacity: 10) ist ein Aufruf des Konstruktors public A(int capacity) während capacity von Namen vorbei

+0

jetzt bekomme ich es, danke – Skoochapp

Verwandte Themen