0

I'm trying declare ref object as optional parameter. So I've understood why I can't do that. The decesion was to overload my method and now I have a new problem:

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

How can I optimize my code?

BWA
  • 5,672
  • 7
  • 34
  • 45
user3818229
  • 1,537
  • 2
  • 21
  • 46
  • 4
    Can I ask a semi-related question, why do you need to pass `EmployeeType` by ref, is it a struct? – CodingGorilla Mar 23 '16 at 16:39
  • You don't assign anything to `employeeType` whatsoever. Why do you even need `ref` modifier here? – Sriram Sakthivel Mar 23 '16 at 16:48
  • @SriramSakthivel Not to copy the object. As I know ref in C# the same thing with & in C++.. – user3818229 Mar 23 '16 at 16:52
  • @HenkHolterman It logs errors. – user3818229 Mar 23 '16 at 16:53
  • @HenkHolterman As I know wothout `ref` it copy object to stack. Am I wrong? – user3818229 Mar 23 '16 at 16:56
  • @user3818229 No it's not the same - `ref` indicates a parameter is passed _by reference_ in C#. Unless you are going to change which object `employeeType` references, you do not need `ref`. It's also unclear what you expest the assignment to `type` to do. Do you intend it to "clone" `employeeType`? – D Stanley Mar 23 '16 at 16:57
  • @user3818229 It will not copy the object unless `EmployeeType` is a `struct`. – D Stanley Mar 23 '16 at 16:57
  • @user3818229 I would suggest you read up on `ref` and the difference between value types and reference types. Don't assume that the concepts and constructs in a managed language like C# are the same as an unmanaged one like C++. – D Stanley Mar 23 '16 at 16:59
  • @DStanley Is there only a one way when I need to use `ref` with struct? – user3818229 Mar 23 '16 at 16:59
  • 1
    @user3818229 Only when using interop, or you want to mutate the struct, but mutable structs can cause all sorts of problems, so they are usually avoided at all costs. – D Stanley Mar 23 '16 at 17:02
  • If you aren't sure, don't use `ref` or `struct`. I don't see why you need them here. – Jodrell Mar 23 '16 at 17:02
  • Regarding [`ref`](http://stackoverflow.com/a/635934/1997232). – Sinatr Mar 23 '16 at 17:03
  • @user3818229 I ceratinly wouldn't do it to avoid "copying to the stack". Don't try to fix memory problems until you are certain there _is_ a problem. – D Stanley Mar 23 '16 at 17:03
  • @DStanley I read information about `ref` and `out`. Everything clear in my mind right now! Thanks everyone! – user3818229 Mar 23 '16 at 17:08

1 Answers1

0

I wouldn't say it's a good (or very bad) idea, but you can create overload without ref and call method requiring one with value which is not used to return:

public Guid GetIdByEmployeeTypeName(string typeName)
{
    var tmp = new EmployeeType();
    return GetIdByEmployeeType(typeName, ref tmp);
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319