can a structure used in a class if yes then how.

Answers were Sorted based on User's Feedback



can a structure used in a class if yes then how...

Answer / jj

This example shows that when a struct is passed to a method,
a copy of the struct is passed, but when a class instance is
passed, a reference is passed.

// struct2.cs
using System;

class TheClass
{
public int x;
}

struct TheStruct
{
public int x;
}

class TestClass
{
public static void structtaker(TheStruct s)
{
s.x = 5;
}
public static void classtaker(TheClass c)
{
c.x = 5;
}
public static void Main()
{
TheStruct a = new TheStruct();
TheClass b = new TheClass();
a.x = 1;
b.x = 1;
structtaker(a);
classtaker(b);
Console.WriteLine("a.x = {0}", a.x);
Console.WriteLine("b.x = {0}", b.x);
}
}

Output

a.x = 1
b.x = 5

From MSDN:

Is This Answer Correct ?    6 Yes 0 No

can a structure used in a class if yes then how...

Answer / radhika

yes we can use the structure in a class.

Is This Answer Correct ?    4 Yes 2 No

Post New Answer

More C Sharp Interview Questions

Why is it a bad idea to throw your own exceptions?

4 Answers  


explain synchronous and asynchronous in C#

3 Answers   AxSys, Mastek, Synechron,


Which program construct must return a value?

0 Answers   3i Infotech,


If you define a user defined data type by using the struct keyword, is it a value type or reference type?

0 Answers  


What is the purpose of the integer parse method the decimal parse method?

0 Answers  






Can you allow class to be inherited, but prevent the method from being over-ridden?

2 Answers   Mind Tree,


Can you change the value of a variable while debugging a C# application?

1 Answers  


the c# keyword .int. Maps to which .net type?

0 Answers   Siebel Systems,


What is the purpose of dependency injection?

0 Answers  


Can partial class be inherited?

0 Answers  


What does virtual keyword mean ?

6 Answers   TCS,


When should I use static in C#?

0 Answers   SwanSoft Technologies,


Categories