June 4, 2007
Hi Guys
I got the following program from the website. Address is given. I couldn’t understand why output a.x 20. Please anybody explain.
Thank you
//http://www.java2s.com/Code/CSharp/Language-Basics/Copyastruct.htm
using System;
// Define a structure.
struct MyStruct
{
public int x;
}
// Demonstrate structure assignment.
public class StructAssignment
{
public static void
{
MyStruct a;
MyStruct b;
a.x = 10;
b.x = 20;
Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);
a = b;
b.x = 30;
Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);
}
}
/*
a.x 10, b.x 20
a.x 20, b.x 30
*/
Posted Jul 30, 2008, 12:40 PM
Thank you for your explanations.
AlanPosted Jul 30, 2008, 11:08 AM
The compiler won't allow you to call an instance method or property (including a setter) on a struct unless all the fields of the struct have been assigned a value. This is a safety feature to ensure that the method or property is not accessing unallocated memory which could contain anything.
When you include this line:
Employee myAssistant = new Employee();
a new struct instance is created with all its fields set to their default values and assigned to the variable myAssistant.
The compiler is then happy that all the fields of the struct have been assigned values and allows the following line to compile:
myAssistant.IDNumber = 345;
Similarly, in Ryan's example, the compiler has no problem with the above line now that IDNumber is a field rather than a property. 'new' is not needed to enable the program to compile.
Ryan AlfordPosted Jul 30, 2008, 10:46 AM
Posted Jul 30, 2008, 9:18 AM
I wish to know why program is not executing without new Employee(); in my program. Please explain the reason.
Ryan AlfordPosted Jul 30, 2008, 8:47 AM
struct Employee
{
public int IDNumber;
}
static void Main()
{
Employee myAssistant;
myAssistant.IDNumber = 345;
Console.WriteLine("ID # is {0}", myAssistant.IDNumber);
}
Posted Jul 30, 2008, 8:34 AM
In the following program even though Employee is Struct if we comment out new Employee(); the program is not executing. Please explain the reason.
using System;
public class CreateEmployee
{
public static void Main()
{
Employee myAssistant; //= new Employee();
myAssistant.IDNumber = 345;
Console.WriteLine("ID # is {0}", myAssistant.IDNumber);
}
}
struct Employee
{
private int idNumber;
public int IDNumber
{
get{ return idNumber; }
set{ idNumber = value; }
}
}
//ID # is 345
ArshadPosted Jul 30, 2008, 8:03 AM
Posted Jul 30, 2008, 7:51 AM
In the above program MyStruct a = new MyStruct(); is not used. That means new operator is allocating the needed memory. Why has new operator not been used? Please explain the reason.
AlanPosted Jun 4, 2007, 11:00 AM
This line:
a = b ;
copies the MyStruct object contained in the variable b to the variable a.
As b.x equals 20 at this point, so does a.x.
The following line:
b.x = 30;
changes b's x field but doesn't change a's x field because 'a' only contains a copy of what 'b' originally contained.
So, when it's printed out, a.x still equals 20.