Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

What is the difference between constants and read-only?

918


What is the default boolean value in c#?

958


Explanation on Generic?

968


Difference between call by value and call by reference in C#?

1020


Explain the difference between access specifier and access modifier in c#?

983


How long can loop recorders stay in?

1173


how to compare numbers and dispaly the largest ? *first thing I wanted to do is to input how many numbers to be compared *and then analyzed the largest then display it.

1732


What are the basics of c#?

1040


What is uint c#?

910


What is int64 in c#?

896


What do you mean by streamreader/streamwriter class in c#?

955


How assembly versioning in .NET prevent DLL Hell problem?

955


What do you mean by saying a "struct is a value type"?

921


What is jit (just in time)?

981


Can we use "this" command within a static method?

999