M
M
Maxim2017-01-29 14:31:25
OOP
Maxim, 2017-01-29 14:31:25

How to change parameter type in C# inherited method?

Task:
The abstract class Stream has a method Stream.Read(Byte[], Int32, Int32). (Standard System.Stream class).
There is a class Question: How to implement Read(char[], Int32, Int32) in MyTCPStream, but not to implement Read (Byte[], Int32, Int32)? PS I know that for interface methods you can do this:
class MyTCPStream : Stream

class E : IIF
{
  pubic string M1() {...}
  IIF.M1(){
  return (object)M1();
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stanislav Makarov, 2017-01-29
@Got_Oxidus

What you are asking cannot be done for one simple reason. Inheritance in .NET is only "real", i.e. when the is-a relation is established. Your MyTCPStream cannot be a Stream unless it implements the Read(Byte[], Int32, Int32) method. Ask yourself - what happens if I cast a MyTCPStream object to a Stream and try to call Read(Byte[]...)?
Of course, you can throw InvalidOperationException in the body of the method, but this is definitely a sign of an incorrectly chosen abstraction. Personally, I don't understand why you might need a TCPStream from which you're not reading bytes, but characters.
To implement the process of reading more complex objects (and a character is a more complicated thing, because its representation in the stream depends on the encoding), DotNet has a TextReader and its descendants. It would be nice if you could describe the initial task - perhaps we will advise you something useful, not weird hacks.

D
Dark Hole, 2017-01-29
@abyrkov

I
will not explain.
Abstract classes, as well as interfaces, are needed to provide the desired syntax. Those. what you are trying to do is a dirty hack. About the obviousness of this kind of code, I, perhaps, will keep silent ...

J
John_Nash, 2017-01-30
@John_Nash

you can not inherit or make the heir abstract

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question