how to use List Class in Base class?

P

phl

Hi, I have a class, which inherits a class that inherits a List class.
How do I pass this base List Class to another function by reference? At
the moment I just create a new List for doing this but I don't want to
do this unless I have to.

cheers


here's some code:
classA : List<string>
{

}

classB: classA
{
function B()
{
//how do I pass the List collection to somefunction here???
VVVVV
somefunction(????, string svar)
}

}

Class SomeClass
{
function somefunction(ref List<string> mylist, string svar)
{
mylist.add(svar);
}
}
 
G

Greg Young

this is a list .. so just pass "this"

somefunction(this, string svar)

btw from the code shown you don't need ref ...

Cheers,

Greg Young
MVP - C#
 
K

Kevin Spencer

//how do I pass the List collection to somefunction here???

Since your claqss inherits List<string>, it *is* a List<string>. So, you
pass your class instance to the method.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
B

Barry Kelly

phl said:
Hi, I have a class, which inherits a class that inherits a List class.
How do I pass this base List Class to another function by reference? At
the moment I just create a new List for doing this but I don't want to
do this unless I have to.

You cannot pass a variable declared as a descendant type (or 'this', for
that matter) to a method which requires a ref parameter, because doing
so would violate type safety. Consider:

class A {}
class B : A {}
class C : A {}

class X
{
static void F(ref A a)
{
a = new C();
}
}

How could you possibly pass a variable of type B to X.F()?

Reference types (i.e. types defined with the 'class' keyword in C#) are
reference types already, and don't require a 'ref' keyword.

-- Barry
 
P

phl

Hi, thanks for the help I got it now.

I want to be efficient, so I don't want to pass by val, hence i try to
use ref. If I don't pass by ref and pass the instance of the class to
the function, would there be alot of overhead? What would be the
overhead of creating a new List everytime I need to call the fucntion
and pass by ref? With the base List having 2 possible scenarios in
mind:

1. Base List has 100s of elements already
2. Base List is empty
 
B

Barry Kelly

phl said:
Hi, thanks for the help I got it now.

I want to be efficient, so I don't want to pass by val, hence i try to
use ref. If I don't pass by ref and pass the instance of the class to
the function, would there be alot of overhead?

Reference ('class') types are already a reference to an object allocated
on the garbage collected heap. Passing a reference type as is (i.e.
without 'ref') is simply copying the native pointer size for the system
(4 bytes for 32-bit systems).
What would be the
overhead of creating a new List everytime I need to call the fucntion
and pass by ref? With the base List having 2 possible scenarios in
mind:

Don't pass by reference at all - you don't need to, unless the function
you're calling needs to actually assign a *different* class instance in
the parameter.

All .NET 'class' types are already passed by reference, and there's
nothing you can do about it to make it less efficient, unless you use a
value type (i.e. a type declared with 'struct' in C#).

When you apply 'ref' to a reference type argument, you're actually
passing a reference to a reference. If you want to think of it at the
machine level, you'd be passing a pointer to a pointer to an instance.

-- Barry
 
J

Jon Skeet [C# MVP]

All .NET 'class' types are already passed by reference, and there's
nothing you can do about it to make it less efficient, unless you use a
value type (i.e. a type declared with 'struct' in C#).

While you clearly understand the actual semantics, calling the default
behaviour "pass by reference" is incorrect and can be confusing. The
reference is passed by value, which is different to a class instance
being passed by reference.
See http://www.pobox.com/~skeet/csharp/parameters.html
 
B

Barry Kelly

Jon Skeet said:
While you clearly understand the actual semantics, calling the default
behaviour "pass by reference" is incorrect and can be confusing. The
reference is passed by value, which is different to a class instance
being passed by reference.

At the machine level, all parameters are passed by value. To implement
pass by reference, a reference must be passed by value, usually in the
form of a pointer.

So the only difference is the level of abstraction you or I am thinking
at. I'm thinking at the machine level.

-- Barry
 
J

Jon Skeet [C# MVP]

Barry Kelly said:
At the machine level, all parameters are passed by value. To implement
pass by reference, a reference must be passed by value, usually in the
form of a pointer.

So the only difference is the level of abstraction you or I am thinking
at. I'm thinking at the machine level.

Whereas I thought we were talking at the language level. At the machine
level there's no such concept as "pass by reference" in the first
place, so if you're thinking at the machine level, why are you saying
that anything is passed by reference?

All I can say is that I've seen plenty of people get confused by the
description of reference type parameters being passed by reference by
default. Their confusion is cleared up when you explain that they're
references which are passed by value. It also makes more sense of the
situation where a reference type parameter really *is* passed by
reference.
 
B

Barry Kelly

Jon Skeet said:
All I can say is that I've seen plenty of people get confused by the
description of reference type parameters being passed by reference by
default.

I agree. I'll be more clear in future.

-- Barry
 

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

Similar Threads


Top