inheritance in value types of = operator overloading in reference types

A

apm

Recently I have had to use a value type for a complex structure because I
don't know how to override the = operator. Can the operator ever be
overloaded? Or can inheritance be used with value types?
 
N

Nicholas Paldino [.NET/C# MVP]

apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to overload
the = operator?

You might be able to get away with what you are doing by overloading the
casting (implicit and explicit).

Hope this helps.
 
A

apm

Nicholas Paldino said:
apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to overload
the = operator?

The = does what I want it to do in a value type. I don't have to overload
it. I just wanted to 'have my cake and eat it too'. Why won't inheritance
work with value types?
You might be able to get away with what you are doing by overloading
the casting (implicit and explicit).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Recently I have had to use a value type for a complex structure because I
don't know how to override the = operator. Can the operator ever be
overloaded? Or can inheritance be used with value types?
 
N

Nicholas Paldino [.NET/C# MVP]

apm,

Structures (value types) can not be inherited from. That's just the way
it is.

So you want copy semantics on assignment. You should just implement the
IClonable interface and have it return the results of MemberwiseClone, as
this will do the same thing that assigning a structure to a variable of that
structure's type does.

Of course, you should do this on a reference type. Doing it on a
structure is a bit redundant.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Nicholas Paldino said:
apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to overload
the = operator?

The = does what I want it to do in a value type. I don't have to overload
it. I just wanted to 'have my cake and eat it too'. Why won't inheritance
work with value types?
You might be able to get away with what you are doing by overloading
the casting (implicit and explicit).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Recently I have had to use a value type for a complex structure because
I don't know how to override the = operator. Can the operator ever be
overloaded? Or can inheritance be used with value types?
 
A

apm

Nicholas Paldino said:
apm,

Structures (value types) can not be inherited from. That's just the
way it is.

So you want copy semantics on assignment. You should just implement
the IClonable interface and have it return the results of MemberwiseClone,
as this will do the same thing that assigning a structure to a variable of
that structure's type does.

Thank you. Forgot I could do that. I'll assume that is the same as
overloding Clone().

Of course, you should do this on a reference type. Doing it on a
structure is a bit redundant.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Nicholas Paldino said:
apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to overload
the = operator?

The = does what I want it to do in a value type. I don't have to overload
it. I just wanted to 'have my cake and eat it too'. Why won't
inheritance work with value types?
You might be able to get away with what you are doing by overloading
the casting (implicit and explicit).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Recently I have had to use a value type for a complex structure because
I don't know how to override the = operator. Can the operator ever be
overloaded? Or can inheritance be used with value types?
 
N

Nicholas Paldino [.NET/C# MVP]

apm,

You can't overload Clone, since object doesn't expose it by default.
You have to implement it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Nicholas Paldino said:
apm,

Structures (value types) can not be inherited from. That's just the
way it is.

So you want copy semantics on assignment. You should just implement
the IClonable interface and have it return the results of
MemberwiseClone, as this will do the same thing that assigning a
structure to a variable of that structure's type does.

Thank you. Forgot I could do that. I'll assume that is the same as
overloding Clone().

Of course, you should do this on a reference type. Doing it on a
structure is a bit redundant.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
in message apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to
overload the = operator?

The = does what I want it to do in a value type. I don't have to
overload it. I just wanted to 'have my cake and eat it too'. Why won't
inheritance work with value types?


You might be able to get away with what you are doing by overloading
the casting (implicit and explicit).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Recently I have had to use a value type for a complex structure
because I don't know how to override the = operator. Can the operator
ever be overloaded? Or can inheritance be used with value types?
 

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