This is not too clear... thougt i got it but now i lost it againstruct/class

C

christery

read something on yoda by Jo I think...

annoying ... not like C I think... thougth I got it but no... what am
I missing...

public struct S
{ public int i;}

public class C
{ public int i;}

class Program
{
static void Main(string[] args){
S a = new S();
C c = new C();

a.i=5;
c.i = 5;

S b = a;
C d = c;

a.i=6;
c.i = 6;

Console.WriteLine(b.i); //5 huh?
Console.WriteLine(d.i); // 6 yess....

Console.ReadLine();
}
}

//CY
 
J

Jon Skeet [C# MVP]

read something on yoda by Jo I think...

annoying ... not like C I think...

It's certainly not like C.
thougth I got it but no... what am I missing...

When you execute S b = a;
then b is a copy of the data in a, which is the actual integer.
Changing the value of a.i doesn't change the value of b.i.

When you execute C d = c;
then (again) d is a copy of the data in b, which is just a reference
to the object. Both variables then refer to the same object, so when
you change c.i, you can "see" that change via d.i as well.

Jon
 
C

christery

J

Jon Skeet [C# MVP]

OK, think I got it... confusing what are references and what are new
copys...

looking at #3 in http://rapidapplicationdevelopment.blogspot.com/
2007/01/parameter-passing-in-c.html
made me think it switched between copy/ref magically...

Nope - the important thing is that assignment *always* copies the value
of the variable; you just need to understand whether the value of the
variable is the actual data, or just a reference. Once that "clicks"
life becomes a lot easier :)
 
C

christery

Nope - the important thing is that assignment *always* copies the value
of the variable; you just need to understand whether the value of the
variable is the actual data, or just a reference. Once that "clicks"
life becomes a lot easier :)

just tried

StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = sb1.Append(" world");
sb1 = null;

Console.WriteLine(sb1); //
Console.WriteLine(sb2); // Hello world

to see what happend, learning by my misstakes, today Im gonna learn
alot ;)

*think* I got the basics of it...

thanks.

//CY
 
M

Marc Gravell

I think that unfortunately you're picking some awkward examples ;-p
If you take out the "sb1=null", you will see "hello world" twice;
actually, sb1 and sb2 both point to the same StringBuilder, which is
the /exact opposite/ of how a normal string concatenation would work.
Sorry.

I've never thought much about why this is the case... presumably so
you can call:
sb.Append('\t').Append(value1).Append('\t').Append("boo");
etc to avoid having to run the token-parsing step of
string.Format(...); and maybe it can optimise the stack usage... I
haven't looked...

Marc
 
C

christery

I think that unfortunately you're picking some awkward examples ;-p
If you take out the "sb1=null", you will see "hello world" twice;
actually, sb1 and sb2 both point to the same StringBuilder, which is
the /exact opposite/ of how a normal string concatenation would work.
Sorry.

I've never thought much about why this is the case... presumably so
you can call:
sb.Append('\t').Append(value1).Append('\t').Append("boo");
etc to avoid having to run the token-parsing step of
string.Format(...); and maybe it can optimise the stack usage... I
haven't looked...

Marc

Sooo... its not gettig clearer, its getting more complicated...
*muttering*, but thanks Marc

any variable is its own, any transfer "=" will create a new, eaven to
an array (string in c is * char) but not here..

tought stringbuilder with "new" is/does/creates an object, and any
variable references to a this is a pointer.

thanks for the info, I´m back where I started then...

//CY
 
C

christery

Sooo... its not gettig clearer, its getting more complicated...

Hmm,,, played around... it cleared...

StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = new StringBuilder(" world");
sb1.Append(sb2);
Console.WriteLine(sb1); // Hello world
sb1 = sb2;
Console.WriteLine(sb1); // world

starting to get it... just took a while.. 8)

//CY
 
C

christery

and naturally

StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = new StringBuilder(" world");
sb1.Append(sb2);
Console.WriteLine(sb1); // Hello world
sb1 = sb2;
Console.WriteLine(sb1); // world
Console.WriteLine(sb2); // world

now I just have to get my head to work that way...

//CY
 
M

Marc Gravell

any variable is its own, any transfer "=" will create a new, eaven to
an array (string in c is * char) but not here..
No; any "=" will reassign the variable. For reference-types (classes),
that doesn't *create* anything.
tought stringbuilder with "new" is/does/creates an object, and any
variable references to a this is a pointer.
The "new StringBuilder" creates an object; the various Append methods
actually end with "return this", so it returns the object that you
were invoking. Kinda pointless, as I mentioned. This is quite the
exception.

Have you read Jon's page on this? It is what the blogspot article you
cited refers to, and is well worth a read...

Marc
 
C

christery

Have you read Jon's page on this? It is what the blogspot article you
cited refers to, and is well worth a read...

Marc

Marc,

Yepp, thats when this all started... found a link to an other side
(eaven with pretty pictures) and then it got a bit worse...

so from http://www.yoda.arachsys.com/csharp/parameters.html to

http://rapidapplicationdevelopment.blogspot.com/2007/01/parameter-passing-in-c.html

and example #3 on the latter is what illudes me... s1 and s2 points to
the same place in memory until we change s2... or so i see it, that
dosent happen i C, a string is always a pointer to bytes 2 pointers
point on the same place will change if you change the data, not split
into two different "strings" (no there are no strings in C, just null
terminated arrays, I know) magically.

Still might just be language problems, thinking swedish and C, reading
english and c#

//CY
 
M

Marc Gravell

that dosent happen i C, a string is always a pointer to bytes 2 pointers
point on the same place will change if you change the data, not split
into two different "strings"

Well actually the pointers description actually fits very well with
what is happening here... apologies if I fluff the function names (or
indeed general C principles), but it has been a decade since I coded
in C...

Imagine that instead of calling strcat, we are calling a different
Concat method that:
* accepts two pointers
* finds the total length (strlen twice?)
* creates a new block for the size (malloc?)
* copies the two strings into the new buffer at different places
(strcpy twice?)
* returns the pointer to the new buffer

then we could write in C (again, apologies for not membering much C
syntax)
blah c = Concat(a,b);
But *equally* you could write
a = Concat(a,b);

In C# terms, this is *broadly* comparable (in terms of what the
results are; not what actually happens) to string concatenation:
a += b;
or
a = a + b;

We've created a new location, and updated an existing variable to
point at it. Does that help at all?

Marc
 
C

christery

Marc,

Yup, just suprised me, written some small programs in C# and never
thougt of this, it worked anyway... (bumbelbee syndrom, dosent know it
cant fly)

no fancy stuff though (the programs).

Thanks all for the postings

/CY
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top