Click here to Skip to main content
15,881,173 members
Home / Discussions / C#
   

C#

 
Question[Message Deleted] Pin
RINSON VARGHESE21-Oct-09 2:40
RINSON VARGHESE21-Oct-09 2:40 
AnswerRe: c# RePost Pin
Richard MacCutchan21-Oct-09 2:42
mveRichard MacCutchan21-Oct-09 2:42 
Question[Message Deleted] Pin
RINSON VARGHESE21-Oct-09 2:39
RINSON VARGHESE21-Oct-09 2:39 
AnswerRe: advantages of c# Pin
Richard MacCutchan21-Oct-09 2:41
mveRichard MacCutchan21-Oct-09 2:41 
GeneralRe: advantages of c# Pin
musefan21-Oct-09 2:47
musefan21-Oct-09 2:47 
QuestionREF AND OUT VARIABLES Pin
RINSON VARGHESE21-Oct-09 2:10
RINSON VARGHESE21-Oct-09 2:10 
AnswerRe: REF AND OUT VARIABLES Pin
Luc Pattyn21-Oct-09 2:17
sitebuilderLuc Pattyn21-Oct-09 2:17 
AnswerRe: REF AND OUT VARIABLES [modified] Pin
musefan21-Oct-09 2:43
musefan21-Oct-09 2:43 
Well an OUT wont compile if you don't set it in the function, a REF does not need settings. Thou, why would you use a REF if you didn't plan on using it anyway. Maybe one path uses it, other path does not.

Maybe a better example would be if you were to re-use the value in other functions, for example consider the following sample...

ref int SomeData;

public void Setup(ref int data)
{
    SomeData = data;
}

public void Add5()
{
   SomeData += 5;
}


...pointless? Yes. Doe's it compile? I don't know. But would not work with the OUT parameter.


[EDIT] It does not compile

Next attempt...

If you where to design a class library that was to be used for somebody else then it could be useful.

You could use REF to ensure that the value was set before passing in, and you could use out if you didn't care what the value was to start with. Examples...

public void (out int i)
{
    i = 10;
}

public void (ref int i)
{
    i = i + 5;//would break if not already set
}


...Maybe this is what Luc was getting at


[EDIT] Just me again...

I worked out a better usage. Consider the following struct instead of a basic datatype.

struct Sample{
   public string A;
   public string B;

   public Sample(string a, string b)
   {
      A = a;
      B = b;
   }   
}


If you use this struct with REF, then you can change B and keep A the same, if you use OUT then you are required to initialize the data and thus lose all data.

This is the same for passing Controls (for whatever reason you would want to)

Life goes very fast. Tomorrow, today is already yesterday.

modified on Wednesday, October 21, 2009 9:12 AM

GeneralRe: REF AND OUT VARIABLES Pin
Luc Pattyn21-Oct-09 4:16
sitebuilderLuc Pattyn21-Oct-09 4:16 
AnswerRe: REF AND OUT VARIABLES Pin
Ghydo21-Oct-09 2:47
Ghydo21-Oct-09 2:47 
GeneralRe: REF AND OUT VARIABLES Pin
Ghydo21-Oct-09 2:53
Ghydo21-Oct-09 2:53 
GeneralRe: REF AND OUT VARIABLES Pin
musefan21-Oct-09 2:55
musefan21-Oct-09 2:55 
GeneralRe: REF AND OUT VARIABLES Pin
Ghydo21-Oct-09 3:04
Ghydo21-Oct-09 3:04 
GeneralRe: REF AND OUT VARIABLES Pin
musefan21-Oct-09 3:08
musefan21-Oct-09 3:08 
GeneralRe: REF AND OUT VARIABLES Pin
Saksida Bojan21-Oct-09 6:12
Saksida Bojan21-Oct-09 6:12 
GeneralRe: REF AND OUT VARIABLES Pin
Luc Pattyn21-Oct-09 4:12
sitebuilderLuc Pattyn21-Oct-09 4:12 
Question.NET SerialPort Pin
hairy_hats21-Oct-09 1:54
hairy_hats21-Oct-09 1:54 
AnswerRe: .NET SerialPort Pin
Luc Pattyn21-Oct-09 2:22
sitebuilderLuc Pattyn21-Oct-09 2:22 
GeneralRe: .NET SerialPort Pin
hairy_hats21-Oct-09 2:55
hairy_hats21-Oct-09 2:55 
GeneralRe: .NET SerialPort Pin
Luc Pattyn21-Oct-09 3:50
sitebuilderLuc Pattyn21-Oct-09 3:50 
QuestionTimer and closing console Pin
abbd21-Oct-09 1:52
abbd21-Oct-09 1:52 
AnswerRe: Timer and closing console Pin
Alan N21-Oct-09 2:00
Alan N21-Oct-09 2:00 
GeneralRe: Timer and closing console Pin
abbd21-Oct-09 2:07
abbd21-Oct-09 2:07 
GeneralRe: Timer and closing console Pin
J4amieC21-Oct-09 2:17
J4amieC21-Oct-09 2:17 
GeneralRe: Timer and closing console Pin
abbd21-Oct-09 2:20
abbd21-Oct-09 2:20 

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.