• Advertisement
Sign in to follow this  

a question concering c#

This topic is 4797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How exactly are objects handled as opposed to Java(all objects are more or less pointers, basic types passed by value)? Ive read some tuts but am still not totally clear on this.

Share this post


Link to post
Share on other sites
Advertisement
It's a little different than Java, because there are structs in C#, as well as the ref and out keywords.

Instances of a class are all passed by reference just like with Java. AFAIK, it all behaves the same. Instances of a struct are passed by value. For example, Microsoft.DirectX.Vector2 is a struct, so if I do this:

Vector2 a=new Vector2();
a.X=a.Y=10;
b=a;
b.x=20;

Then at the end, a.X!=b.X - when you do b=a, a copy is made. If you want to pass a reference to a struct to a method, you use the ref keyword. So if I do this:

private void change(ref Vector2 v) { v.x=10; }

Vector a=new Vector2();
a.X=20;
change(ref a);

then at the end, a.X=10.

The out keyword has the same sort of effect on references - they allow a method to change what object a reference points to.

There's more on all this in the C# spec, including some really good examples: see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/CSharpSpecStart.asp

Happy coding :)

Share this post


Link to post
Share on other sites
Quote:
Original post by CProgrammer
How exactly are objects handled as opposed to Java(all objects are more or less pointers, basic types passed by value)?
Ive read some tuts but am still not totally clear on this.

In C#, there are two types of variables -- value types and reference types.

Value types contain their data directly. A simple declaration like "int i = 4;", for instance, declares an integer called i whose value is 4. A value type is any of the following:

-- struct type: built-in types and structs, comprising ...
     ...numeric types: integral types, floating-point types, decimal
     ...user-defined structs
     ...bool
-- enumeration type: user-defined enumerable types

Reference types contain a reference to their data. A declaration like "ForumUser f = new ForumUser("CProgrammer");" declares a reference to an object of type ForumUser. A reference type is any of the following:

-- built-in reference type: object or string
-- user-defined reference type: anything declared with class (classes), interface (interfaces), or delegate (delegates)

By default, all parameters are passed by value by default. What "passing by value" means is subtly different for each of the two types of variables. Suppose we pass a value-type and a reference-type variable by both value and reference in several different methods? What are the effects?


Variable type Parameter passed by ... Effect on original variable?
------------- ----------------------- ---------------------------------
Value-type Passed by value No. Changes to parameter do
not affect original variable.

Passed by reference Yes. Changes to parameter will
affect original variable.

Ref-type Passed by value Partial. Changes to the data
pointed to by the reference
will affect the original. But
reference itself cannot be
modified to point to a new
object.

Passed by reference Yes. Changes to parameter will
affect original variable.


A more in-depth discussion, including the syntax for passing by reference and by value, can be found on MSDN here.

HTH,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement