Oh, thats exactly how it works, thanks for the correction. :)Quote:
Originally Posted by 7stud
Printable View
Oh, thats exactly how it works, thanks for the correction. :)Quote:
Originally Posted by 7stud
7stud, so in C# you can achieve the same result using "ref" in your function parameters. This will pass a variable by reference, otherwise it makes a copy of the passed object (pass by value).
I'm not familiar with the syntax of C#. From what I hear, it's supposed to be a Java like language, so it might work the same way as Java: you just pass the object name(which is a pointer so only an address gets copied).Quote:
7stud, so in C# you can achieve the same result using "ref" in your function parameters. This will pass a variable by reference, otherwise it makes a copy of the passed object (pass by value).
Works the opposite it seems. If you don't specify the "ref" before the variable in the method def, it will copy the object into the function. If you use the ref keyword, you will be manipulating the object sent to the function.Quote:
Originally Posted by 7stud
Based on this description of 'out' and 'ref':Quote:
Originally Posted by CompiledMonkey
http://www.c-sharpcorner.com/Language/out_and_ref.asp
I think you are misunderstanding some basics. Primitive types like int and double are not "objects" so their names are not pointers. When you pass a primitive type in Java or C#, you are "passing by value". If you understand "passing by value" and its implications, i.e. you can't change the original variable, then 'out' and 'ref' allow you to do some things you can't do when passing by value in C++. However, in C++ you don't have to pass primitives by value. In C++, you can achieve the same thing as 'ref' by passing a pointer to the primitive type.
Yes, I understand that int/double/etc are primitive types, but I assumed we're talking about user defined data types, such as my Book object above. Which, when used with ref:
will manipulate the actual Book object you passed in, rather than copy the object and only have its scope visible in that function.Code:public void Blah(ref Book b) {}
Sorry, that reference I cited used 'ref' in conjunction with primitive types, so I thought they didn't apply to objects. Are you saying you can't alter 'b' without using 'ref'?Quote:
Originally Posted by CompiledMonkey
Yes.Quote:
Originally Posted by CompiledMonkey
Yes.Code:If you don't specify the "ref" before the variable in the method def, it will copy the...
and yes for the rest.
That is exactly the same as its done in C++.
ref Book b == Book* b.
Correct. If you don't specify ref, it will always get passed by value. You can always return the Book type, so:Quote:
Originally Posted by 7stud
But that seems pointless, so just use ref in that case.Code:public Book Blah(Book b)
{
// do some stuff to b
return b;
}
obj.b = Class.Blah(obj.b);
Thats right. Thats how its done in C++, and what was the question again? ;)Quote:
Originally Posted by CompiledMonkey
Haha, I think we've just been enjoying a learning session here. I've learned a lot. :)Quote:
Originally Posted by Dae
I read up on C# types and parameter passing, and from what I can tell if you pass an object of a user defined class to a function, then you are passing a pointer, so the function can use that pointer to change the original object.Quote:
Quote:
Originally Posted by 7stud
Quote:
Originally Posted by CompiledMonkey
----Quote:
Simple types (such as float, int, char), enum types and struct types are all value types.
Class types, interface types, delegate types and array types are all reference types.
Yes, the tutorial mentions this:Quote:
Originally Posted by CompiledMonkey
However, when you pass a pointer/reference by value, the function gets a copy of the address--but a copy of an address can still access the original object (pointers also get passed by value in C++; it's just called "passing by reference"). Here is an example from the tutorial I read:Quote:
There are four different kinds of parameters in C#: value parameters (the default), reference parameters (which use the ref modifier)...
Can you get that example to work?Quote:
(I have used StringBuilder as a random example of a reference type - there's nothing special about it.)Code:void Foo (StringBuilder x)
{
x.Append (" world");
}
...
StringBuilder y = new StringBuilder();
y.Append ("hello");
Foo (y);
Console.WriteLine (y);
output: hello world
According to the tutorial, since your Book object is a class:
you shouldn't have to use 'ref' to be able to change the original Book object. However, if Book were a struct then you would have to use 'ref' to change the original object because structs are classified with the primitive types in C#.Quote:
but I assumed we're talking about user defined data types, such as my Book object above
Wow, have I missed that all this time?!?!?! That example did work and I also created a simple Book example too.
Book.cs
Program.csCode:namespace Answers
{
public class Book
{
private int year;
public int Year
{
get
{
return this.year;
}
set
{
this.year = value;
}
}
}
}
Code:public static void Main(string[] args)
{
Program p = new Program();
StringBuilder y = new StringBuilder();
y.Append("hello");
p.Foo(y);
Console.WriteLine(y);
Book b = new Book();
b.Year = 2005;
Console.WriteLine(b.Year);
p.ChangeYear(b);
Console.WriteLine(b.Year);
System.Console.ReadLine();
}
public void Foo(StringBuilder x)
{
x.Append(" world");
}
public void ChangeYear(Book b)
{
b.Year = 1999;
}
:)Quote:
Wow, have I missed that all this time?!?!?!
Now try this:
Explain the result.Code:public void Foo(StringBuilder x)
{
x = null;
}
public static void Main(string[] args)
{
StringBuilder y = new StringBuilder();
y.Append("hello");
Foo(y);
Console.WriteLine(y);
}
What does this do in C#?
Code:Program p = new Program();
Weird, that just printed out "hello", so it didn't nullify the StringBuilder from above.Quote:
Originally Posted by 7stud
The class all of this code was in is called Program. So I had to make a reference since the methods outside of main are not static.Quote:
Originally Posted by 7stud