Which is better in this case, return or correct

suppose I have the following group of static functions

here I sent the variable via the link:

public static void ProcessEmailMessage(ref string HTML)
        {
            ModifyLinks(ref HTML);
            AddFakeImage(ref HTML);
        }

        public static void ModifyLinks(ref string HTML)
        {
            //modify HTML links 
        }


        public static void AddFakeImage(ref string HTML)
        {
            //adds an image to the HTML
        }

and here I sent a variable by value

public static string ProcessEmailMessage(string HTML)
        {
            HTML = ModifyLinks(HTML);
            HTML = AddFakeImage(HTML);
            return HTML;
        }

        public static string ModifyLinks(string HTML)
        {
            //modify HTML links
            return HTML;
        }


        public static string AddFakeImage(string HTML)
        {
            //adds an image to the HTML
            return HTML;
        }

which makes more sense, and is there a performance difference between 2?

+5
source share
6 answers

Avoid using options and options if possible.

Methods that use the ref and out parameters are more difficult to use, you need to declare a variable to store the result, and the semantics are a little more difficult to understand. The difference in performance (if any) will be negligible.

The analysis of the code in Visual Studio most likely gives a warning for their use in this case.

. http://msdn.microsoft.com/en-us/library/ms182131.

+8

, ref , . , , .

, , , , , :

return AddFakeImage(ModifyLinks(HTML));

ref , . .

+2

. .

, , . , ( , , , ).

+1

, "". "ref".

, , :

ref:

string modifyMe = GetMeString();
ModifyLints(ref modifyMe);
AddFakeImage(ref modifyMe);

:

string result = AddFakeImage(ModifyLinks(GetMeString()));
+1

If you need to return multiple objects, use ref/ out, if you can do it with returnjust returning one object, use return.
refmakes your code unnecessarily hidden and creates overhead for writing, which is not necessary in this case.
You should always try to keep your code simple and overview.

+1
source

I prefer something like a builder who does:

new EmailMessageProcessor (html) .WithModifiedLinks (). WithFakeImages ().

There are no ref arguments, and everything is encapsulated.

+1
source

All Articles