c# - Returning incorrect type from a function -


OK, I was scanning through some code yesterday and I saw it,

Code:

  Public class MyBaseClass {public string Field0 () {Return string. Empty; }} Public Class Mikloss: Mibesclass {public string field 1 () {return string.opt; }}  

There is no easy complaint in the main code,

code:

  Private zero form1_load (object sender, event events E) {} Private MySQL GateSomething () {var retVal = new MyClass (); Return rate; }  

Despite a return type MyBaseClass in the GetSomthing () function, it actually allows the return of MyClass (I think it is due to Warsaw) < / Pre>

/ P>

I saw it in the cell ales code and it seems bad practice for me, though I'm missing something, does anyone have any ideas?

Why is this poor practice? This is absolutely fine. For example, you may want:

  Public INIM Marketplace & lt; String & gt; GetNames () {New list & lt; String & gt; {"Fred", "John", "Joe"}; }  

This makes sense from an encapsulation point - you're isolating the interface that is ready to guarantee the collar (this By implementing (you are using a list & lt; string & gt; ) this lets you change the implementation later without disturbing the call.

This good practice bad instead of practice


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -