So the question is what is the point of passing a reference type along with the ref keyword?
Scenario 1:
I have an Employee class as below.
public class Employee
{
In my calling class, I say:
class Program
{
After having a look at the code, you’ll probably say,
Well, an instance of a class gets passed as a reference, so any changes to the instance inside the CallSomeMethod, actually modifies the original object. Hence the output will be ‘John-Doe’ on the first call and ‘Smith-Doe’ on the second.
And you’re right:
So the question is what’s the use of passing this Employee parameter as a ref?
Read more: IBloggable - implemented
Scenario 1:
I have an Employee class as below.
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return string.Format("{0}-{1}", FirstName, LastName);
}}
In my calling class, I say:
class Program
{
static void Main()
{
Employee employee = new Employee
{FirstName = "John",LastName = "Doe"};
Console.WriteLine(employee);CallSomeMethod(employee);Console.WriteLine(employee);
}
private static void CallSomeMethod(Employee employee)
{
employee.FirstName = "Smith";employee.LastName = "Doe";
}}
After having a look at the code, you’ll probably say,
Well, an instance of a class gets passed as a reference, so any changes to the instance inside the CallSomeMethod, actually modifies the original object. Hence the output will be ‘John-Doe’ on the first call and ‘Smith-Doe’ on the second.
And you’re right:
So the question is what’s the use of passing this Employee parameter as a ref?
Read more: IBloggable - implemented