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 275839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:49:46+00:00 2026-05-12T00:49:46+00:00

I was working on a demo method and found something strange(at least to me

  • 0

I was working on a demo method and found something strange(at least to me :-))

enter code here  class Program
{
    public void AnotherSwap<T>(T a, T b)  
    {
        T temp;
        temp = a;
        a = b;
        b = temp;

        Console.WriteLine(a);
        Console.WriteLine(b);

    }


    public void swap<T>(T a, T b) where T : MyInt  // Passing without ref
    {
        object temp;
        temp = a.MyProperty;
        a.MyProperty = b.MyProperty;
        b.MyProperty = (int)temp;

        Console.WriteLine(a.MyProperty);
        Console.WriteLine(b.MyProperty);



    }

    static void Main(string[] args)
    {
        Program p = new Program();
        MyInt a = new MyInt() { MyProperty = 10 };
        MyInt b = new MyInt() { MyProperty = 20 };
        p.swap<MyInt>(a, b);
        Console.WriteLine(a.MyProperty); // changed values get reflected
        Console.WriteLine(b.MyProperty); // changed values get reflected

        Console.WriteLine("Another Swap");
        object x = 10;
        object y = 20;
        p.AnotherSwap(x, y);

        Console.WriteLine(x);  // changed values are not getting  reflected
        Console.WriteLine(y);  // changed values are not getting  reflected

         Console.ReadKey();


    }
   public class MyInt
    {
        public int MyProperty { get; set; }

    }




}

Here when I am calling the swap() , though I haven’t mentioned ref , the changed values are automatically getting reflected (as in p.swap(a, b); a and b are instance of Myint and thus will by default work as ref..as per my understanding.)
But same should happen with Anotherswap() here also I am passing object x,y but now the values are not getting reflected in Main() .i.e. now its working as a value type.
Can somebody explain where my understanding is going wrong.
Let me know if u guys want more info.

  • 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-05-12T00:49:47+00:00Added an answer on May 12, 2026 at 12:49 am

    When MyInt type is cast as Object, it gets cast as a val type. Object cannot be treated like a reference since its representing both val and ref, it must be explicitly passed by ref.

    int can be cast as an object, and its still treated as a val.
    string can be cast as object, and its treated as a val. Even though normally it would be a ref type.

    If you did the AnotherSwap and passed T in as MyInt, you would get the same results as before. If you called the original swap algorithm with objects, you would get the same output as the latter.

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

Sidebar

Related Questions

I'm working on legacy code that looks very similar to this: public class BaseParser
I'm working on a demo about collision detection. (Some of the code for this
I am using Slidesjs it working fine in below url http://www.mysoko.in/demo/Simple/ And same code
I can not access public method of my class MyPanel which extends JPanel .
(I asked a similar question here and found a working solution under certain circumstances,
I am working on Rails 3.1 Jquery Ajax demo app listed here https://github.com/samnang/ajax_rails31_demo .
I'm working with a demo of two sortable jquery lists... but have a problem
Working on a carousel lightbox tooltip demo and as far I can see in
Possible Duplicate: Avoid resending forms on php pages Index.html <form method=post action=demo.php> <input type=text
i have been working on a small demo and i wrote a function which

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.