Click here to Skip to main content
15,915,702 members
Home / Discussions / C#
   

C#

 
QuestionReflection, Breakpoints and Debug Mode Assembllies Pin
Tristan Rhodes24-Nov-05 5:58
Tristan Rhodes24-Nov-05 5:58 
AnswerRe: Reflection, Breakpoints and Debug Mode Assembllies Pin
leppie24-Nov-05 11:00
leppie24-Nov-05 11:00 
QuestionProblem with events Pin
sciamachy24-Nov-05 5:48
sciamachy24-Nov-05 5:48 
AnswerRe: Problem with events Pin
Leslie Sanford24-Nov-05 6:58
Leslie Sanford24-Nov-05 6:58 
QuestionTreeView question. Pin
zaboboa24-Nov-05 4:26
zaboboa24-Nov-05 4:26 
AnswerRe: TreeView question. Pin
Robert Rohde24-Nov-05 4:39
Robert Rohde24-Nov-05 4:39 
QuestionMethod parameters Pin
1nsp1r3d24-Nov-05 3:15
1nsp1r3d24-Nov-05 3:15 
AnswerRe: Method parameters Pin
Leslie Sanford24-Nov-05 3:40
Leslie Sanford24-Nov-05 3:40 
1nsp1r3d wrote:
As I was reading C# Language Pocket Reference, it says that all parameters are passed by value(ASAIK the book targets .NET 1.0).


Correct, unless the ref modifier is used.

1nsp1r3d wrote:
I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)? Is that the case?


Nope. Smile | :)

When a reference variable is passed to a method, it is by default passed by value. So if you were to do something like this:

public void SomeMethod(string name)
{
    name = "B";

    // Local name now points to "B"
}


Only the local name variable is changed. The reference variable passed to the method is not changed:

//...

string name = "A";

SomeMethod(name);

// name still references "A"


So when a reference variable is passed to a method, it is passed by value by default. A copy of the variable is what is actually passed to the method. This local copy references the same object in memory as the original variable, but they are two distinct variables. Changes on the local variable itself does not change the original.

However, if you use the ref modifier, you have a reference to a reference, so to speak. So given our above example:

public void SomeMethod(ref string name)
{
    name = "B";

    // name points to "B"
}

//...

string name = "A";

SomeMethod(ref name);

// name now points to "B"


Changes to the variable itself are reflected in the original.
GeneralRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 4:15
Colin Angus Mackay24-Nov-05 4:15 
AnswerRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 4:25
Colin Angus Mackay24-Nov-05 4:25 
GeneralRe: Method parameters Pin
Leslie Sanford24-Nov-05 4:46
Leslie Sanford24-Nov-05 4:46 
GeneralRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 4:58
Colin Angus Mackay24-Nov-05 4:58 
GeneralRe: Method parameters Pin
Leslie Sanford24-Nov-05 5:34
Leslie Sanford24-Nov-05 5:34 
GeneralRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 5:44
Colin Angus Mackay24-Nov-05 5:44 
AnswerRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 5:02
Colin Angus Mackay24-Nov-05 5:02 
QuestionData types in reports (rdls) Pin
doph24-Nov-05 1:50
doph24-Nov-05 1:50 
QuestionConverting decimal to String Pin
Subrahmanyam K24-Nov-05 1:37
Subrahmanyam K24-Nov-05 1:37 
AnswerRe: Converting decimal to String Pin
J4amieC24-Nov-05 2:02
J4amieC24-Nov-05 2:02 
AnswerRe: Converting decimal to String Pin
Craig G Fraser24-Nov-05 2:04
Craig G Fraser24-Nov-05 2:04 
QuestionRegarding Strong name Pin
A.Grover24-Nov-05 1:23
A.Grover24-Nov-05 1:23 
AnswerRe: Regarding Strong name Pin
S. Senthil Kumar24-Nov-05 2:19
S. Senthil Kumar24-Nov-05 2:19 
GeneralRe: Regarding Strong name Pin
A.Grover24-Nov-05 23:14
A.Grover24-Nov-05 23:14 
Questionclient/server communication Pin
batmanAgen24-Nov-05 1:08
batmanAgen24-Nov-05 1:08 
QuestionGenetating XML Pin
Gktony24-Nov-05 0:56
Gktony24-Nov-05 0:56 
AnswerRe: Genetating XML Pin
J4amieC24-Nov-05 1:33
J4amieC24-Nov-05 1:33 

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.