2016-03-23 3 views
0

J'essaie de déclarer ref object comme paramètre optionnel. J'ai donc compris pourquoi je ne peux pas faire ça. Le decesion était de surcharger ma méthode et maintenant j'ai un nouveau problème:Est-il possible de ne pas copier ref vers variable locale?

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)); 
} 

Comment puis-je optimiser mon code?

+4

Puis-je poser une question semi-connexe, pourquoi avez-vous besoin de passer 'EmployeeType' par ref, est-ce une struct? – CodingGorilla

+0

Vous n'attribuez rien à 'employeeType'. Pourquoi avez-vous même besoin du modificateur 'ref' ici? –

+0

@SriramSakthivel Ne pas copier l'objet. Comme je sais ref en C# la même chose avec & en C++ .. – user3818229

Répondre

0

Je ne dirais pas qu'il est un bon (ou très mauvaise) idée, mais vous pouvez créer une surcharge sans ref et la méthode d'appel nécessitant une avec une valeur qui ne sert pas à revenir:

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