Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone tell me what this code is doing and why it won\'t run? Must use metho

ID: 3750405 • Letter: C

Question

Can anyone tell me what this code is doing and why it won't run?

Must use method noName and can add more or change ref to non-ref or non-ref to ref, using different StringBuilder methods.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodArgs
{
    class MethodArgs
    {
        public static long noName(long a, ref long b, long c, String s, StringBuilder t)
        {
            long d;
            d = a + b + c;
            a = a + 1;
            b = b + 1;
            t = t.Insert(0, s);
            s = "hi";
            return (d);
        }
        static void Main(string[] args)
        {
            long a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 0;
            String x = "Hello";
            System.Text.StringBuilder sb;  // fully qualified
            sb = new StringBuilder(" mom", 100);
            Console.WriteLine("a = " + a + " b = " + b + " c = " + c + " d = " + d + " g = " + g);
            Console.WriteLine("sb = "+ sb + " x = " + x);
            g = noName(a, ref b, c, x, sb);
            System.Console.WriteLine("a = " + a + " b = " + b + " c = " + c + " d = " + d + " g = " + g);
            Console.WriteLine("sb = " + sb + " x = " + x);

            g = MethodArgs.noName(d, ref e, f, x, sb);
            System.Console.WriteLine("a = " + a + " b = " + b + " c = " + c);
            System.Console.WriteLine("d = " + d + " e = " + e + " f = " + f + " g = " + g);
            Console.WriteLine("sb = " + sb + " x = " + x);

            g = MethodArgs.noName(b, ref c, a, "HI ", sb);
            System.Console.WriteLine("a = " + a + " b = " + b + " c = " + c);
            System.Console.WriteLine("d = " + d + " e = " + e + " f = " + f + " g = " + g);
            Console.WriteLine("sb = " + sb + " x = " + x);
            Console.ReadKey();
        }
    }
}

Explanation / Answer

/* Read all the comments, you will get the logic and reason behind output */

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace MethodArgs

{

class MethodArgs

{

/* This is method which is returning long data to calling statement */

/* Here variable b is reference variable, hence any change in variable 'b' has final change*/

/* StringBuilder t has object reference, hence that change is also global change*/

public static long noName(long a, ref long b, long c, String s, StringBuilder t)

{

long d;

d = a + b + c; //d is assigned value of a,b and c

a = a + 1; //Here a is incremented by 1, but this is local change, hence

//changed value of 'a' is not final change

b = b + 1; // Here b is incremented by 1, but this is ref variable , hence

//changed value of 'b' is final change

t = t.Insert(0, s); // variable t is StringBuilder object pointing the actual value,

//string variable is append to this

// Hence change will be final change with stringbuilder

s = "hi"; //Here s is assigned as "hi", but this is local change, hence

//changed value of 's' is not final change

return (d); //at the end , d as a final result is returned

}

static void Main(string[] args)

{

  

  

//Here a,b,c,d,e,f,g are local variables with their initilized value

long a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 0;

String x = "Hello";

System.Text.StringBuilder sb; // fully qualified

sb = new StringBuilder(" mom", 100);

//Printing value

Console.WriteLine("a = " + a + " b = " + b + " c = " + c + " d = " + d + " g = " + g);

Console.WriteLine("sb = "+ sb + " x = " + x);

  

g = noName(a, ref b, c, x, sb);

System.Console.WriteLine("a = " + a + " b = " + b + " c = " + c + " d = " + d + " g = " + g);

Console.WriteLine("sb = " + sb + " x = " + x);

g = MethodArgs.noName(d, ref e, f, x, sb);

System.Console.WriteLine("a = " + a + " b = " + b + " c = " + c);

System.Console.WriteLine("d = " + d + " e = " + e + " f = " + f + " g = " + g);

Console.WriteLine("sb = " + sb + " x = " + x);

g = MethodArgs.noName(b, ref c, a, "HI ", sb);

System.Console.WriteLine("a = " + a + " b = " + b + " c = " + c);

System.Console.WriteLine("d = " + d + " e = " + e + " f = " + f + " g = " + g);

Console.WriteLine("sb = " + sb + " x = " + x);

Console.ReadKey();

}

}

}

/* Here is the output */

a = 1 b = 2 c = 3 d = 4 g = 0
sb = mom x = Hello
a = 1 b = 3 c = 3 d = 4 g = 6
sb = Hello mom x = Hello
a = 1 b = 3 c = 3
d = 4 e = 6 f = 6 g = 15
sb = HelloHello mom x = Hello
a = 1 b = 3 c = 4
d = 4 e = 6 f = 6 g = 7
sb = HI HelloHello mom x = Hello