Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
No Overload takes 5 arguments
Message
De
21/09/2013 08:16:19
 
 
À
21/09/2013 07:24:56
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01583574
Message ID:
01583810
Vues:
34
>>>>>>>But unless the called method needs to access existing values using 'ref' is a waste of bandwidth.
>>>>>>
>>>>>>Hadn't thought about that but with and empty varchar going in for an errormessage are we really talking about a lot of bandwidth?
>>>>>
>>>>>In that instance - No. But still unnecessary. But if, for example, the parameter is a large object which needs to be marshalled then it is more significant.
>>>>>I'd argue that, just from a best-practice POV, if the called method doesn't require the parameter to be passed in then 'out' is best.
>>>>
>>>>I see your point. With the out param, where does the return value show up in the debugger? I don't remember what it was I ran into that drew me to the by ref but it seems it had to something to do with debugging. The out param is not defined before the call to the function, is it? I really need to look at all that again as I realize now there is some stuff there I'm fuzzy on having not used that signature for a while.
>>>
>>>One oddity with the debugger that I noticed when the param IS assigned a value:
   private static void Main()
>>>    {
>>>        string x = "Hello";
>>>        Doit(out x);
>>>    }
>>>
>>>    static void Doit(out string s)
>>>    {
>>>        //Debugger shows 's' as Hello
>>>        //Watch window allows 's+" Mate"' etc
>>>        // But not in code
>>>    
>>>        s = "GoodBye";
>>>    }
>>
>>Ok, it's coming back to me now. This and the guideline Craig cites is what scared me away from out params. I'm not 100% convinced the bandwidth argument is compelling in any use-case I've seen. If I found myself needing something that big back as a param from a function I think I might start to look carefully at my design.
>I think we're debating two different (but related things) here.
>(a) Whether to ever use 'out' or 'ref'
>(b) If we are using either then which to choose.
>
>Choice on the latter seems straightforward to me : If the value does not need to be passed in then use 'out'. Whether the performance difference is minimal or not may make little difference in most cases - but 'out' is still best.
>
>>Do you have an example in mind?
>Four ways of returning two objects:
    private static void Main()
>    {
>        OneBigObject o1;
>        AnotherBigObject o2;
>        if (GetObjectsWithOut(out o1, out o2))
>        {
>            //OK
>        }
>
>        o1 = new OneBigObject();
>        o2 = new AnotherBigObject();
>
>        if (GetObjectsWithRef(ref o1, ref o2))
>        {
>            
>        }
>
>        BothObjects bo = GetBothObjects();
>        if (bo != null)
>        {
>            //OK
>        }
>
>        Tuple<bool,OneBigObject, AnotherBigObject> tuple = GetTupleObjects();
>
>        if (tuple.Item1)
>        {
>            o1 = tuple.Item2;
>            o2 = tuple.Item3;
>        }
>    }
>
>
>    public static Tuple<bool,OneBigObject, AnotherBigObject> GetTupleObjects()
>    {
>        //Ignoring any exception.....
>        return  new Tuple<bool,OneBigObject, AnotherBigObject>(true,new OneBigObject(),new AnotherBigObject());
>    }
>
>    public static bool GetObjectsWithOut(out OneBigObject o1, out AnotherBigObject o2)
>    {
>        o1 = null;
>        o2 = null;
>        try
>        {
>            o1 = new OneBigObject();
>            o2 = new AnotherBigObject();
>            return true;
>        }
>        catch (Exception)
>        {
>            return false;
>        }
>    }
>
>    public static bool GetObjectsWithRef(ref OneBigObject o1, ref AnotherBigObject o2)
>    {
>        return true;
>    }
>
>    public static BothObjects GetBothObjects()
>    {
>        try
>        {
>            return new BothObjects{o1=new OneBigObject(),o2= new AnotherBigObject()};
>        }
>        catch (Exception)
>        {
>            return null;
>        }
>    }
>}
>
>public class OneBigObject
>{
>}
>
>public class AnotherBigObject
>{
>}
>
>public class BothObjects
>{
>    public OneBigObject o1 { get; set; }
>    public AnotherBigObject o2 { get; set; }
>}
'ref' is just wasteful. GetBothObjects() requires creating a separate class as a container. GetTupleObjects() is less readable. I like 'out' best....


Why do you think ref is wasteful, Viv ?

There's - technically - no difference between out and ref

Calling : Both push the address of the 'item' on the stack

Returning: Both use a store indirect to put the 'item' at that address
objects : stind.ref ( http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.stind_ref.aspx )
value types : depending on the size of the valute type
4 bytes : eg stdind.i4 http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.stind_i4.aspx
8 bytes : eg stdint.I8 http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.stind_i8.aspx
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform