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

Related Questions

I have written the code below for a login page, but doesn't seem to
Hi I have written below code function unique(th){ var obj = {}; for(var i
I am trying to fetch wcf service from jquery. I have written below code
The code below is written in php: $user = addslashes($_POST['user']); $pwd = addslashes($_POST['pwd']); $query
Below, I inserted a code written by Ray Konopka (part of the Coderage presentation).
Below is some code I've written that is effective, but makes too many database
I have written grammar for a language (sample code below) //this is a procedure
I have written a MapReduce program, code is below: import java.io.IOException; import java.util.Iterator; import
Which code snippet will give better performance? The below code segments were written in
I am writing a very simple HTML code which is listed below. Written in

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.