Click here to Skip to main content
15,887,410 members
Home / Discussions / C#
   

C#

 
Questionslow in filling data in datagridview Pin
jojoba201127-Apr-11 4:03
jojoba201127-Apr-11 4:03 
AnswerRe: slow in filling data in datagridview Pin
Blue_Boy27-Apr-11 4:14
Blue_Boy27-Apr-11 4:14 
AnswerRe: slow in filling data in datagridview Pin
Rob Philpott27-Apr-11 4:21
Rob Philpott27-Apr-11 4:21 
AnswerRe: slow in filling data in datagridview Pin
R. Giskard Reventlov27-Apr-11 4:25
R. Giskard Reventlov27-Apr-11 4:25 
GeneralRe: slow in filling data in datagridview Pin
OriginalGriff27-Apr-11 8:25
mveOriginalGriff27-Apr-11 8:25 
AnswerRe: slow in filling data in datagridview Pin
thatraja27-Apr-11 4:35
professionalthatraja27-Apr-11 4:35 
AnswerRe: slow in filling data in datagridview Pin
Dhyanga27-Apr-11 7:12
Dhyanga27-Apr-11 7:12 
QuestionA question about references Pin
6,921,364 and growing27-Apr-11 3:32
6,921,364 and growing27-Apr-11 3:32 
In C# - Classes are reference types; structures are value types.
I was reading about this and saw something which I didn't understand.

I have created a simple program to explain my doubt. In this program I have defined a class and a structure with an integer variable.
C#
class TestClass
{
    public int x;
}
struct TestStruct
{
    public int x;
}


Now I have couple of static methods each changing the value of the integer.
C#
static void ChangeStruct(TestStruct ts)
{
    ts.x = 10;
}
static void ChangeClass(TestClass tc)
{
    tc.x = 10;
    //tc = null;
}

Now in the main method, I call this functions and results are as expected.
C#
static void Main(string[] args)
{
    TestStruct ts = new TestStruct();
    ts.x = 20;
    Console.WriteLine("Before change: ts is ({0})", ts.x);
    ChangeStructure(ts);
    Console.WriteLine("After method: ts is ({0)", ts.x);
    TestClass tc = new TestClass(); 
    tc.x = 20;
    Console.WriteLine("Before method: tc is ({0})", tc.x);
    ChangeClass(tc);
    Console.WriteLine("After method: tc is ({0})", tc.x);
    Console.ReadLine();
}

So far so good. Now when I uncomment the line tc = null; in the method ChangeClass, I expect the class to be null referenced i.e it should not reference to any memory in the heap any more.
But that is not how it works.

Can anybody explain me why there is a different behaviour here.

Thanks a lot!
AnswerRe: A question about references Pin
Rob Philpott27-Apr-11 3:44
Rob Philpott27-Apr-11 3:44 
GeneralRe: A question about references Pin
6,921,364 and growing27-Apr-11 18:19
6,921,364 and growing27-Apr-11 18:19 
GeneralRe: A question about references Pin
6,921,364 and growing27-Apr-11 18:34
6,921,364 and growing27-Apr-11 18:34 
AnswerRe: A question about references Pin
Ian Shlasko27-Apr-11 3:46
Ian Shlasko27-Apr-11 3:46 
AnswerRe: A question about references Pin
6,921,364 and growing27-Apr-11 18:31
6,921,364 and growing27-Apr-11 18:31 
GeneralRe: A question about references Pin
Ian Shlasko28-Apr-11 1:36
Ian Shlasko28-Apr-11 1:36 
GeneralRe: A question about references Pin
6,921,364 and growing28-Apr-11 3:33
6,921,364 and growing28-Apr-11 3:33 
AnswerRe: A question about references Pin
V.27-Apr-11 3:52
professionalV.27-Apr-11 3:52 
GeneralRe: A question about references Pin
Ian Shlasko27-Apr-11 3:54
Ian Shlasko27-Apr-11 3:54 
QuestionOpinion on the following please... Pin
Rob Philpott27-Apr-11 3:15
Rob Philpott27-Apr-11 3:15 
AnswerRe: Opinion on the following please... Pin
Pete O'Hanlon27-Apr-11 3:17
mvePete O'Hanlon27-Apr-11 3:17 
GeneralRe: Opinion on the following please... Pin
Rob Philpott27-Apr-11 3:24
Rob Philpott27-Apr-11 3:24 
AnswerRe: Opinion on the following please... Pin
Ian Shlasko27-Apr-11 3:19
Ian Shlasko27-Apr-11 3:19 
GeneralRe: Opinion on the following please... Pin
Paladin200027-Apr-11 3:30
Paladin200027-Apr-11 3:30 
GeneralRe: Opinion on the following please... Pin
Rob Philpott27-Apr-11 3:36
Rob Philpott27-Apr-11 3:36 
GeneralRe: Opinion on the following please... Pin
Paladin200027-Apr-11 3:43
Paladin200027-Apr-11 3:43 
AnswerRe: Opinion on the following please... Pin
PIEBALDconsult27-Apr-11 14:30
mvePIEBALDconsult27-Apr-11 14:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.