Click here to Skip to main content
15,924,036 members
Home / Discussions / C#
   

C#

 
GeneralRe: Can’t show controlBox/System menu when FormBorderStyle=None. Pin
Rey999917-Nov-06 7:57
Rey999917-Nov-06 7:57 
QuestionPointer typecasting Pin
Dewald16-Nov-06 3:41
Dewald16-Nov-06 3:41 
AnswerRe: Pointer typecasting Pin
Judah Gabriel Himango16-Nov-06 4:52
sponsorJudah Gabriel Himango16-Nov-06 4:52 
GeneralRe: Pointer typecasting Pin
Dewald17-Nov-06 3:58
Dewald17-Nov-06 3:58 
GeneralRe: Pointer typecasting Pin
Judah Gabriel Himango17-Nov-06 5:13
sponsorJudah Gabriel Himango17-Nov-06 5:13 
GeneralRe: Pointer typecasting Pin
Dewald20-Nov-06 20:37
Dewald20-Nov-06 20:37 
GeneralRe: Pointer typecasting Pin
Dewald21-Nov-06 1:19
Dewald21-Nov-06 1:19 
GeneralRe: Pointer typecasting Pin
Judah Gabriel Himango21-Nov-06 5:28
sponsorJudah Gabriel Himango21-Nov-06 5:28 
Yeah, to pass a reference of anything into a method, just use the ref keyword.

However, arrays are reference types themselves. When you pass it into a method, it's passing a pointer to the real data:

byte[] myBytes = { 5, 10, 15 };
FillItAllWith7s(myBytes);
// myBytes is now filled with all 7s.

...

void FillItAllWith7s(byte[] bytesToModify)
{
    for(int i = 0; i < bytesToModify.Length; i++)
    {
        bytesToModify[i] = 7;
    }
}


Notice how I didn't even need to use the ref keyword; passing any class type (AKA reference type) into a function simply passes a pointer to that object. Passing any struct type (AKA value type, including int, double, float, etc.) will pass a copy of the struct instance to the function. This is when the ref keyword really comes in handy.

However, you can certainly use the ref keyword for parameters that are classes/reference types, as you just did in your above example. This allows you to actually change the reference itself:

byte[] myBytes = {1, 2, 3};
SetToNull(ref myBytes);
// myBytes is now null! We actually changed the myBytes to point to something else!

...

void SetToNull(ref byte[] bytesToModify)
{
   bytesToModify = null;
}


To summarize, if you're just changing the data of the class passed in as a parameter to a function, you don't need the ref keyword. In fact, I think I've used the ref keyword -- for reference type parameters -- on functions maybe 2 or 3 times in my 500k line project at work. I've used the ref keyword many times for value type parameters.

The only time you *need* the ref keyword on classes/reference types passed in as parameter is if you need to actually modify what the object points to. In your case, if you needed to change the byte[] to point to something else (that is, a new byte[], or a null), then you'd use the ref keyword. Otherwise, you can omit the ref keyword and pass in the array if you just want to manipulate that array (i.e. change the array's elements). Make sense?


Tech, life, family, faith: Give me a visit.
I'm currently blogging about: God-as-Judge, God-as-Forgiver
The apostle Paul, modernly speaking: Epistles of Paul

Judah Himango


QuestionSandcastle Help File Builder with hyperlinks Pin
Christopher Stratmann16-Nov-06 3:25
Christopher Stratmann16-Nov-06 3:25 
AnswerRe: Sandcastle Help File Builder with hyperlinks Pin
Colin Angus Mackay16-Nov-06 3:46
Colin Angus Mackay16-Nov-06 3:46 
QuestionRe: Sandcastle Help File Builder with hyperlinks Pin
Christopher Stratmann16-Nov-06 4:29
Christopher Stratmann16-Nov-06 4:29 
AnswerRe: Sandcastle Help File Builder with hyperlinks Pin
Pete O'Hanlon16-Nov-06 5:02
mvePete O'Hanlon16-Nov-06 5:02 
GeneralRe: Sandcastle Help File Builder with hyperlinks Pin
Christopher Stratmann16-Nov-06 5:12
Christopher Stratmann16-Nov-06 5:12 
Questionhow to read extended/summary properties of an mp3 Pin
pnslcs16-Nov-06 2:55
pnslcs16-Nov-06 2:55 
AnswerRe: how to read extended/summary properties of an mp3 Pin
Colin Angus Mackay16-Nov-06 3:14
Colin Angus Mackay16-Nov-06 3:14 
QuestionStream Reader Pin
Frank Kerrigan16-Nov-06 2:13
Frank Kerrigan16-Nov-06 2:13 
AnswerRe: Stream Reader Pin
Colin Angus Mackay16-Nov-06 2:14
Colin Angus Mackay16-Nov-06 2:14 
GeneralRe: Stream Reader Pin
Frank Kerrigan16-Nov-06 2:15
Frank Kerrigan16-Nov-06 2:15 
GeneralRe: Stream Reader Pin
Alois Kraus16-Nov-06 2:21
Alois Kraus16-Nov-06 2:21 
GeneralRe: Stream Reader Pin
Colin Angus Mackay16-Nov-06 2:25
Colin Angus Mackay16-Nov-06 2:25 
GeneralRe: Stream Reader Pin
Colin Angus Mackay16-Nov-06 2:22
Colin Angus Mackay16-Nov-06 2:22 
GeneralRe: Stream Reader Pin
Frank Kerrigan16-Nov-06 2:23
Frank Kerrigan16-Nov-06 2:23 
AnswerRe: Stream Reader Pin
Judah Gabriel Himango16-Nov-06 4:45
sponsorJudah Gabriel Himango16-Nov-06 4:45 
QuestionUsing globalization for decimal seperator Pin
Yustme16-Nov-06 1:53
Yustme16-Nov-06 1:53 
AnswerRe: Using globalization for decimal seperator Pin
Anthony Mushrow16-Nov-06 2:06
professionalAnthony Mushrow16-Nov-06 2:06 

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.