Camillia
asked on
Explain code - value vs reference type
I got the third one wrong . I have my explanation in the code.
protected void Page_Load(object sender, EventArgs e)
{
string weather = "rainy";
ChangeTheString(weather);
var p = weather; //*** this comes out as "rainy" because string is a reference type so even tho the function changes the value, we'll still get "rainy"
string[] rainydays = new[] {"Monday", "Friday"};
ChangeTheArray(rainydays);
var t = rainydays; //*** this comes out as "Monday" and "Sunday". string array is a value type? (I don't think this is correct even tho I got the answer right)
Forecast forecast = new Forecast {Temp = 700, Pressure = 20};
ChangeTheClassInstance(forecast);
var s = forecast.Temp; //*** this is 35. I got this wrong. I a new instance of Forecast is created and passed. If "class" is a reference type, why didn't I get 700?
}
public class Forecast
{
public int Temp { get; set; }
public int Pressure { get; set; }
}
public static void ChangeTheString(string weather)
{
weather = "sunny";
}
public static void ChangeTheArray(string[] rainyDays)
{
rainyDays[1] = "Sunday";
}
public static void ChangeTheClassInstance(Forecast forecast)
{
forecast.Temp = 35;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. This is a good practice for my Friday's interview. I passed the technical phone interview. Missed a couple of questions but they were ok with that.
I'll look at what you guys have posted to understand this better.
I'll look at what you guys have posted to understand this better.
ASKER
good links and explanation. Thanks
ASKER