2016-03-23 11 views
0

Ich versuche, ref object als optionalen Parameter zu deklarieren. Also habe ich verstanden, warum ich das nicht tun kann. Die decesion war zu meiner Methode überlastet und jetzt habe ich ein neues Problem:Ist es möglich Ref nicht in lokale Variable zu kopieren?

public Guid GetIdByEmployeeTypeName(string typeName) 
{ 
    return SurroundWithTryCatch(() => new EmployeeType().GetEmployerGroupIdByTypeName(typeName)); 
} 

public Guid GetIdByEmployeeTypeName(string typeName, ref EmployeeType employeeType) 
{ 
    EmployeeType type = employeeType; //The problem here. I can not use ref object inside an anonymous method. 
    return SurroundWithTryCatch(() => type.GetEmployerGroupIdByTypeName(typeName)); 
} 

Wie kann ich meinen Code zu optimieren?

+4

Kann ich eine frage Semi-verwandte Frage, warum müssen Sie 'EmployeeType' durch ref übergeben, ist es eine Struktur? – CodingGorilla

+0

Sie ordnen "employeeType" überhaupt nichts zu. Warum brauchst du hier sogar 'ref' Modifier? –

+0

@SriramSakthivel Das Objekt nicht kopieren. Wie ich weiß, ref in C# die gleiche Sache mit & in C++ .. – user3818229

Antwort

0

ich nicht sagen, es wäre eine gute (oder sehr schlecht) ist Idee, aber man kann ohne ref und Call-Methode mit dem Wert erfordern eine Überlastung zu schaffen, die zurück nicht verwendet wird:

public Guid GetIdByEmployeeTypeName(string typeName) 
{ 
    var tmp = new EmployeeType(); 
    return GetIdByEmployeeType(typeName, ref tmp); 
}