473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
Dec 18 '07 #1
12 1199
On Dec 18, 10:49 am, christ...@gmail.com wrote:
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
Dec 18 '07 #2
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
OK, think I got it... confusing what are references and what are new
copys...

looking at #3 in http://rapidapplicationdevelopment.b...sing-in-c.html
made me think it switched between copy/ref magically...

//CY
Dec 18 '07 #3
<ch*******@gmail.comwrote:
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

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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 18 '07 #4
On 18 Dec, 14:00, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
<christ...@gmail.comwrote:
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
OK, think I got it... confusing what are references and what are new
copys...
looking at #3 inhttp://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 :)

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk
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
Dec 18 '07 #5
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
Dec 18 '07 #6
On 18 Dec, 15:22, "Marc Gravell" <marc.grav...@gmail.comwrote:
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

Dec 18 '07 #7
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
Dec 18 '07 #8
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
Dec 18 '07 #9
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
Dec 18 '07 #10
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.b...sing-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
Dec 19 '07 #11
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
Dec 19 '07 #12
On 19 Dec, 17:36, Marc Gravell <marc.grav...@gmail.comwrote:
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
Dec 20 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DraguVaso | last post by:
Hi, I'm having the following situation: - A class clsFournisseur with public property's which raise a MyPropertyChanged-event in the Set-method for each Property. Public Event NomChanged As...
3
by: DraguVaso | last post by:
Hi, I'm having the following situation: - A class clsFournisseur with public property's which raise a MyPropertyChanged-event in the Set-method for each Property. Public Event NomChanged As...
3
by: DraguVaso | last post by:
Hi, I'm having the following situation: - A class clsFournisseur with public property's which raise a MyPropertyChanged-event in the Set-method for each Property. Public Event NomChanged As...
14
by: Charles Jenkins | last post by:
I want to make a MenuItem that I can 'click' programmatically. I started by coding this: class ClickableMenuItem : MenuItem { public void DoClick( EventArgs e ) { OnClick( e ); } ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.