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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is c sharp free?

711


What is the difference between static class and sealed class in c#?

669


What does .length do in c#?

713


What does char mean in c#?

694


How do you declare an interface in c#?

646


What is a protected class c#?

660


What does static mean in c sharp?

788


Should I use double or float?

675


What is lazy in c#?

660


Why do we need interface in c#?

703


What is a property in c#?

658


What is difference between assembly and namespace?

642


how to prevent a class from being inherited in c#.net?

692


What is the base class from which all value types are derived?

708


Which operator cannot be overloaded in c sharp?

654