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
Where are value types stored in c#?
Why do we use dictionary in c#?
What are the benefits of using windows services:
What is Implementation inheritance
Explain how to implement delegates in c#.net
Explain about multithreading?
How is a strongly-named assembly different from one that isn’t strongly-named?
What are constants in c#?
What you mean by delegate in c#?
What is an enumerator c#?
Expalin the way you implement inheritance by using VB.NET/C#?
What do you mean by casting a data type?
If the interface in c# only contains the declaration of the methods and we need to define those methods in the class, then why we use the interface?
What is iformatprovider in c#?
What is action in c# 3.5?