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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:33:54+00:00 2026-05-15T15:33:54+00:00

Why below written code doesn’t gives error even when string is a reference type.

  • 0

Why below written code doesn’t gives error even when string is a reference type.

public struct example
{
   public int a;
   public string name;
};

public void usestruct()
{
example objExample= new example();
MessageBox.Show(objExample.name);
}

EDIT

Modifying Jon Answer, I have few more Questions.

            public struct Example 
            { 
               public int a; 
               public string name; 
            } 

            class Test 
            { 
                static void Main() 
                { 
   //Question 1
                    Example t1 = new Example(); 
                    t1.name = "First name"; 
                    Example t2 = t1; 
                    t2.name = "Second name"; 

                    Console.WriteLine(t1.name); // Prints "First name" 
                    Console.WriteLine(t2.name); // Prints "Second name"
                    Console.WriteLine(t1.name); // What will it Print ????

   //Question 2
                    Example t1 = new Example(); 
                    t1.name = "First name"; 
                    Console.WriteLine(sizeof(t1)); // What will it Print ???? 
                       // I think it will print 4 + 4 bytes. 
                       //4 for storing int, 4 for storing address of string reference.
    //Considering 
    //int of 4 bytes 
    //Memory Address of 4 byte 
    //Character of 1 byte each.
                      //My collegue says it will take 4+10 bytes 
                      //i.e. 4 for storing int, 10 bytes for storing string.
                } 
            } 

How many bytes will second case take.

  • 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-15T15:33:55+00:00Added an answer on May 15, 2026 at 3:33 pm

    The struct just contains a reference to a string. Why would that cause a problem? A reference is just a value. When you copy the structure’s value (e.g. with assignment, or passing it to a method) the reference will be copied too. Note that if you change the value of the field in one copy of the struct, that won’t change the value in a different copy:

    using System;
    
    public struct Example
    {
       public int a;
       public string name;
    }
    
    class Test
    {
        static void Main()
        {
            Example t1 = new Example();
            t1.name = "First name";
            Example t2 = t1;
            t2.name = "Second name";
    
            Console.WriteLine(t1.name); // Prints "First name"
            Console.WriteLine(t2.name); // Prints "Second name"        
        }
    }
    

    If Example were a class instead, these would both print “Second name” as the values of t1 and t2 would be references to the same instance.

    This isn’t specific to strings, either – any reference type will work.

    However, I would strongly advise against creating mutable structs or exposing public fields.

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

Sidebar

Ask A Question

Stats

  • Questions 494k
  • Answers 494k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You either need to re-run the plugin after your elements… May 16, 2026 at 11:00 am
  • Editorial Team
    Editorial Team added an answer Source Control Management is mainly about reproducibility: are you able… May 16, 2026 at 11:00 am
  • Editorial Team
    Editorial Team added an answer I can't see any value in this but if you… May 16, 2026 at 11:00 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I written a code for downloading and saving images from a site .It worked
I've written some code to upgrade a SQL Server database. Before I upgrade the
I have written some C++ code that generates a std::vector. I also have a
Below is the code to write to a file after downloading an image from
i have a procedure in which the below condition is to be written in
Below is my code: if(settings.controlNav){ $('.nivo-controlNav', slider).hide(); slider.hover(function(){ $('.nivo-controlNav', slider).show('slow'); }, function(){ $('.nivo-controlNav', slider).hide('slow');
I've got a multiple step webform written in c#/asp.net. I'm trying to change the
I am trying to write some processor independent code to write some files in
I've written an index and search view all in one if a GET request
I've written a Powershell script which will compare two databases and come up with

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.