Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7924603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:50:10+00:00 2026-06-03T17:50:10+00:00

class Program { static void Main(string[] args) { A a = new A(); a.print();

  • 0
class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        a.print();

    }
}

class Employee
{
    public string Name { get; set; }
}

class A
{
    public void print()
    {
        Employee emp = new Employee { Name = "1" };
        Func2(emp);

        Console.WriteLine(emp.Name);
        Console.ReadLine();
    }

    private void Func2(Employee e)
    {
        Employee e2 = new Employee { Name = "3" };
        e = e2;

    }

}

After running the Above program, I got “1” as answer, which I’m not able to understand HOW? Can anyone explain, the answer according to me should be “3”
-Thanks

But when I call Func1 method which is defined below:-

private void Func1(Employee e)
{
    e.Name = "2";
}

I get “2” as answer. Now if e was passed as Value type then how come it’s giving me “2” as answer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:50:11+00:00Added an answer on June 3, 2026 at 5:50 pm

    Here is the bit that is getting you regarding Func2:

    private void Func2(Employee e)
    {
        Employee e2 = new Employee { Name = "3" };
        e = e2;
    }
    

    Employee is a reference type (a class), but the reference itself is passed by value – it is a copy of the reference.

    You are then assigning to this copy a new reference, but the original reference (that was copied from) did not change. Hence, you are getting a 1.

    If you pass the reference itself by reference, you can modify it:

    private void Func2(ref Employee e)
    {
        Employee e2 = new Employee { Name = "3" };
        e = e2;
    }
    

    The above will produce 3, as you expected.


    Update, regarding your added Func1:

    The reference is a copy, but is still pointing to the same object – you are changing the state of this object (setting the Name property), not the underlying object reference itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class Program { static void Main(string[] args) { new
class Program { static void Main(string[] args) { List<Book> books = new List<Book> {
Suppose public class Program { public static void Main(string[] args) { Program p =
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
class Program { static void Main(string[] args) { string value = "12345"; Type enumType
I am trying to do the following: class Program { static void Main(string[] args)
First look at this code: class Program { static void Main(string[] args) { var
In C#, I can do this: class Program { static void Main(string[] args) {
What is the workaround to this problem? class Program { static void Main(string[] args)
class Program { internal delegate int CallBack(int i); static void Main(string[] args) { CallBack

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.