Re: Inheritance of constructors.
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: Mon, 30 Jun 2008 09:32:08 -0700 (PDT)
On Jun 30, 5:09 pm, A n g l e r <p|k|o|n|i|u|...@xxxxxxxxxxxxxxxxxxx>
wrote:
> Avoiding
making fields public helps too :)
Bare in mind that was just for sake of example.
Unfortunately the duplication of variable names made it hard for me to
see what you actually meant :(
I'm also not sure where threading
comes into it particularly...
It just comes in the sense that you wouldn't like your data collection
being modified while reading. In case of "reference by value" that's not
a problem cos it never gets updated anyway - just remains stuck with an
old collection unless you do something on your own, lol.
No, the collection itself can be changed without changing which
collection the variable refers to.
Imagine a class which handles rendering at 15 frames a second. Now,
imagine that you have a collection of objects it renders to screen or
wherever you fancy. Do you really desire to inform the class that some
objects are added/removed from the data collection? I'd rather have a
memory-wise reference that assures the class would always see the
updated collection. What's the point in copying reference values of
let's say 1000 objects 15 times a second (provided collection changes so
dynamically)?
Um, you will. You can change the contents of the array or another
collection. Here's a short example:
using System;
class Foo
{
string[] names;
public Foo(string[] names)
{
this.names = names;
}
public void ShowNames()
{
Console.WriteLine("Names:");
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
class Test
{
static void Main()
{
string[] x = {"Jon", "Holly"};
Foo foo = new Foo(x);
foo.ShowNames();
x[0] = "Robin";
x[1] = "William";
foo.ShowNames();
}
}
The changes (made in Test) to the array are visible in Foo, because
they both share a reference to the same array. The same would be true
for other reference types such as List<T>.
Now in the display updating example you'd need to potentially be
careful with threading, yes - but it's not too bad a problem.
Given how the above works, why would I want to use "ref" in the
constructor signature? Bear in mind that the "ref" would only affect
the parameter itself anyway...
Jon
.
- Follow-Ups:
- Re: Inheritance of constructors.
- From: A n g l e r
- Re: Inheritance of constructors.
- References:
- Inheritance of constructors.
- From: A n g l e r
- Re: Inheritance of constructors.
- From: Jon Skeet [C# MVP]
- Re: Inheritance of constructors.
- From: A n g l e r
- Re: Inheritance of constructors.
- From: Jon Skeet [C# MVP]
- Re: Inheritance of constructors.
- From: A n g l e r
- Re: Inheritance of constructors.
- From: A n g l e r
- Re: Inheritance of constructors.
- From: Jon Skeet [C# MVP]
- Re: Inheritance of constructors.
- From: A n g l e r
- Re: Inheritance of constructors.
- From: Jon Skeet [C# MVP]
- Re: Inheritance of constructors.
- From: A n g l e r
- Inheritance of constructors.
- Prev by Date: What are SZ array?
- Next by Date: Re: csc or al
- Previous by thread: Re: Inheritance of constructors.
- Next by thread: Re: Inheritance of constructors.
- Index(es):
Relevant Pages
|