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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:31:29+00:00 2026-05-11T21:31:29+00:00

I have a variant on the by ref question. I know all about calling

  • 0

I have a variant on the by ref question. I know all about calling with the ref or our parameters and how it affects variables and their values. I had this issue with a DataTable and I want to know why a datatable is different than a simple integer variable.

I have the physical answer on how to fix the problem but I want to know why it works the way it does.

If you use simple variables it does what I expected

        int mVar1 = 1;
        int mVar2 =1;

        mVar2 = mVar1;

        mVar2 = 5;

        Console.WriteLine(mVar1.ToString());
        Console.WriteLine(mVar2.ToString());

it displays
1,5
in the console.

BUT if you do the same thing with a DataTable it makes a reference to the first datatable instead of a new value:

        DataTable mVar3 = new DataTable();
        DataTable mVar4 = new DataTable();


         // Create DataColumn objects of data types.
        DataColumn colString = new DataColumn("StringCol");
        colString.DataType = System.Type.GetType("System.String");
        mVar3.Columns.Add(colString);

        // Create DataColumn objects of data types.
        DataColumn colString2 = new DataColumn("StringCol123");
        colString2.DataType = System.Type.GetType("System.String");
        mVar4.Columns.Add(colString2);

        foreach (DataColumn tCol in mVar3.Columns)
        {
            Console.WriteLine(tCol.ColumnName);

        }
        foreach (DataColumn tCol in mVar4.Columns)
        {
            Console.WriteLine(tCol.ColumnName);

        }

                mVar4 = mVar3;

        //change mVar4 somehow and see if mVar3 changes


        foreach (DataColumn tCol in mVar4.Columns)
        {
            tCol.ColumnName = "Test";

        }

        foreach (DataColumn tCol in mVar3.Columns)
        {
            Console.WriteLine(tCol.ColumnName);

        }

        foreach (DataColumn tCol in mVar4.Columns)
        {
            Console.WriteLine(tCol.ColumnName);

        }

The console displays:
StringCol
StringCol123
Test
Test

by saying mVar4 = mVar3 it causes mVar4 to be a reference to mVar3.

The solution to this problem is to say

DataTable mVar4 = mVar3.Copy(); 

So my question is: What causes a datatable to perform differently than a simple integer field. Why does it create a reference when I use mVar4 = mVar3 instead of a different copy of the DataTable?

  • 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-11T21:31:30+00:00Added an answer on May 11, 2026 at 9:31 pm

    You’re running up against the difference between a reference type and a value type.

    Here’s an msdn article on differences.

    A more descriptive answer would be that both are actually performing the same operation, the difference is that in the first example (the two integers) the assignment of mVar2 = mVar1 is assigning the value of mVar1 to mVar2, which is 1. However, in the case of the DataTable, what’s actually being assigned is a memory location, not a DataTable.

    Say, for instance, that the DataTable you created resides in memory location 20. That would mean that the reference mVar1 would hold a reference to that location (20). When you do the assignment mVar2 = mVar1, you’re telling mVar2 to hold the same value as mVar1, so mVar2 then references to memory location 20 as well. The result being that both variables reference the same DataTable.

    In order to achieve the behavior you’re describing, you would indeed need to have a Copy ability, as you’ve stated. It would have to allocate an entirely new object, and copy the state of the previous object to the new one.

    For the DataTable class you could extend it like this inside an extension method:

    public static DataTable Copy(this DatTable original)
    {
       var result = new DataTable();
       //assume Property1 was a property of a DataTable
       result.Property1 = original.Property1; 
       //continue copying state from original to result
       return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.