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

4. Describe the process when we send a request URL? And who is responsible for that?

1558


What is the advantage of extension method in c#?

553


How many constructors can a class have in c#?

577


What is c# used for?

610


What is meaning of type safety in c#?

546






Why is lazy loading?

567


What is desktop GUI application?

584


List the two important objects of ado.net and also list the namespaces that are commonly used in ado.net to aid in connection to a database.

604


What is the difference between ienumerable and iqueryable?

535


What is ilasm.exe used for?

579


How to open a new form on button click in Windows forms?

621


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

567


What is difference between managed and unmanaged code?

568


What are the types in c#?

594


What does the dispose method do with the connection object?

614