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

  • Home
  • SEARCH
  • 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 8672377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:11:19+00:00 2026-06-12T19:11:19+00:00

If you create a class in .NET it will be a reference type. In

  • 0

If you create a class in .NET it will be a reference type. In this example I’m setting the name of MyClass inside of SomeMethod. Now inside the Main method I do not really need to assign back the return value from SomeMethod because the parameter passed in is a reference type. I’m wanting the instance in the main method to have the new updated name “John” and it does.

I often assignee back the return value anyway for readability, even though this isn’t needed to get the updated properties reassigned to my origin instance in main. My assumption is that the compiler is smart enough not to recreate a new reference of MyClass inside the Main method. Is that true? Or does the compiler actually create a new reference and pointing to the updated values when assigning itself the return value?

//My class will be a reference type
public class MyClass 
{
    public int ID { get; set; }
    public string name { get; set; }
}

public class Main
{
    //Main method of application
    public void Main()
    {
        MyClass myClass = new MyClass();
        //return value back to itself
        myClass = SomeMethod(myClass);
    }

    public MyClass SomeMethod(MyClass myClass)
    {
        myClass.name = "John";
        return myClass;
    }
}

Here is the IL

    instance void Main1 () cil managed 
{
    // Method begins at RVA 0x207c
    // Code size 15 (0xf)
    .maxstack 2
    .locals init (
        [0] class Test.MyClass myClass
    )

    IL_0000: newobj instance void Test.MyClass::.ctor()
    IL_0005: stloc.0
    IL_0006: ldarg.0
    IL_0007: ldloc.0
    IL_0008: call instance class Test.MyClass Test.Main::SomeMethod(class Test.MyClass)
    IL_000d: stloc.0
    IL_000e: ret
} // end of method Main::Main1
  • 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-12T19:11:20+00:00Added an answer on June 12, 2026 at 7:11 pm

    To answer your question, yes the compiler is smart enough to not reassign the return value to the same object. If we open your code with Reflector or dotPeek, your code :

    public void Main()
    {
        MyClass myClass = new MyClass();
        //return value back to itself
        myClass = SomeMethod(myClass);
    }
    

    is simply transform to :

    public void Main()
    {
        this.SomeMethod(new MyClass());
    }
    

    Even if we do something with the reassign variable, it is still remove by the compiler. For example, the compiler transform this :

    public void Main()
    {
        MyClass myClass = new MyClass();
        //return value back to itself
        myClass = SomeMethod(myClass);
    
        Console.WriteLine(myClass.name);
    }
    

    to that :

    public void Main()
    {
        Console.WriteLine(this.SomeMethod(new MyClass()).name);
    }
    

    But like Lews Therin and Robert Jeppesen said, you shouldn’t do that because it is bad practice and could lead to misunderstanding

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

Sidebar

Related Questions

I am trying to create a class in VB.NET which inherits a base abstract
NET MVC and i want to create a class to contain site wide functions
I am trying to use the System.Net.Mail.MailMessage class in C# to create an email
In .NET, integer data type is a value type(stack) and String is a reference
I have a class library project in .net say A which when built will
The situation is like this : Main project A. and a class library B.
I am created class diagram in asp.net mvc. I drag and droped some of
I have created an ASP.NET class derived from the standard WebControls.TextBox , with the
I created a asp.net website. and added a class file to it. i wrote
I'm creating new Site Definitions using this method: http://weblogs.asp.net/paulballard/archive/2007/04/09/creating-a-custom-sharepoint-2007-portal-site-definition-using-the-portalprovisioningprovider-class.aspx and when they get created,

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.