By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,983 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,983 IT Pros & Developers. It's quick & easy.

how to use List Class in Base class?

P: n/a
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);
}
}

May 12 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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#
"phl" <ki************@hotmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
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);
}
}

May 12 '06 #2

P: n/a
> //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.

"phl" <ki************@hotmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
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);
}
}

May 12 '06 #3

P: n/a
"phl" <ki************@hotmail.com> wrote:
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
May 12 '06 #4

P: n/a
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

May 12 '06 #5

P: n/a
"phl" <ki************@hotmail.com> wrote:
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
May 12 '06 #6

P: n/a
Barry Kelly <ba***********@gmail.com> wrote:

<snip>
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 12 '06 #7

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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
May 12 '06 #8

P: n/a
Barry Kelly <ba***********@gmail.com> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 13 '06 #9

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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
May 13 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.