Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
PIEBALDconsult6-Feb-16 5:23
mvePIEBALDconsult6-Feb-16 5:23 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Member 120616006-Feb-16 7:15
Member 120616006-Feb-16 7:15 
QuestionMessage Closed Pin
4-Feb-16 22:34
ginsa vaheed4-Feb-16 22:34 
AnswerRe: Can we developed c# code for Information KIOSK.? Pin
Pete O'Hanlon4-Feb-16 23:04
mvePete O'Hanlon4-Feb-16 23:04 
AnswerRe: Can we developed c# code for Information KIOSK.? Pin
F-ES Sitecore4-Feb-16 23:43
professionalF-ES Sitecore4-Feb-16 23:43 
GeneralRe: Can we developed c# code for Information KIOSK.? Pin
Richard Deeming5-Feb-16 3:50
mveRichard Deeming5-Feb-16 3:50 
GeneralRe: Can we developed c# code for Information KIOSK.? Pin
F-ES Sitecore5-Feb-16 4:02
professionalF-ES Sitecore5-Feb-16 4:02 
Questionwhen inheritance is ignored ? the limited scope of using 'new with methods Pin
BillWoodruff4-Feb-16 22:12
professionalBillWoodruff4-Feb-16 22:12 
I understand this code: [^]
C#
class Base
{
   public static void F() {}
}
class Derived: Base
{
   new private static void F() {}   // Hides Base.F in Derived only
}
class MoreDerived: Derived
{
   static void G() { F(); }         // Invokes Base.F
}
And, I can follow MS's explanation:
"In the example above, the declaration of F in Derived hides the F that was inherited from Base, but since the new F in Derived has private access, its scope does not extend to MoreDerived. Thus, the call F() in MoreDerived.G is valid and will invoke Base.F."
But, in terms of why method over-riding with 'new is defined as limited in scope in terms of inheritance: I can't say I understand that. And, I am curious about that ... note that my working assumption is that the creators of C# are (infinitely) smarter than I'll ever be, and had good reasons to make the design decisions they did.

While I think the right way to achieve method "hiding" in the context of inheritance is using Interfaces, I also have (as some QA posters express it) "a doubt" about that.

To make this less "theoretical," let me pose a scenario where you have a class where the constructor has a boolean flag; and, you have methods in the class some of which you would like to be unavailable depending on the value of the boolean flag:
C#
public class SomeClass
{
    private bool HideThis;

    public SomeClass(int someInt, bool hideThis = false)
    {
         HideThis = hideThis;
         HideThat = hideThat; 
    }

    public void Method1(int someint)
    {
        if(HideThis) return; // or throw error ?

        // whatever
    }
}
An obvious strategy is just to use a flag to skip executing code in a Method (as shown above). But, what would be interesting is some code where the constructor of the Class is, somehow, a "factory" and returns a class with/without a given method depending on the boolean flag.

While I am sure you could use reflection and compiler services, to generate classes on-the-fly, I would not want to use that unless that was absolutely required for other reasons. And, using System.Dynamic is another route I avoid. I am also aware of using the "Obsolete" attribute to make methods non-browsable; and, that's another place I don't want to go.

The various "factory method" patterns I've seen do not seem to me be usable for this type of scenario ... unless you are willing to do specific casting of the whatever new "object" is returned from the factory to a specific Interface.

thoughts ?
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”

AnswerRe: when inheritance is ignored ? the limited scope of using 'new with methods Pin
Eddy Vluggen4-Feb-16 23:56
professionalEddy Vluggen4-Feb-16 23:56 
AnswerRe: when inheritance is ignored ? the limited scope of using 'new with methods Pin
Sascha Lefèvre5-Feb-16 0:41
professionalSascha Lefèvre5-Feb-16 0:41 
AnswerRe: when inheritance is ignored ? the limited scope of using 'new with methods Pin
John Torjo6-Feb-16 2:13
professionalJohn Torjo6-Feb-16 2:13 
SuggestionRe: when inheritance is ignored ? the limited scope of using 'new with methods Pin
Sascha Lefèvre6-Feb-16 14:29
professionalSascha Lefèvre6-Feb-16 14:29 
GeneralRe: when inheritance is ignored ? the limited scope of using 'new with methods Pin
John Torjo6-Feb-16 14:42
professionalJohn Torjo6-Feb-16 14:42 
QuestionWrite log from multiple instances of an application. Pin
NJdotnetdev3-Feb-16 2:59
NJdotnetdev3-Feb-16 2:59 
AnswerRe: Write log from multiple instances of an application. Pin
Richard Deeming3-Feb-16 4:02
mveRichard Deeming3-Feb-16 4:02 
GeneralRe: Write log from multiple instances of an application. Pin
NJdotnetdev3-Feb-16 4:25
NJdotnetdev3-Feb-16 4:25 
GeneralRe: Write log from multiple instances of an application. Pin
Richard MacCutchan3-Feb-16 4:46
mveRichard MacCutchan3-Feb-16 4:46 
GeneralRe: Write log from multiple instances of an application. Pin
NJdotnetdev3-Feb-16 7:45
NJdotnetdev3-Feb-16 7:45 
GeneralRe: Write log from multiple instances of an application. Pin
Richard Deeming3-Feb-16 9:16
mveRichard Deeming3-Feb-16 9:16 
AnswerRe: Write log from multiple instances of an application. Pin
Daniel Pfeffer3-Feb-16 5:50
professionalDaniel Pfeffer3-Feb-16 5:50 
AnswerRe: Write log from multiple instances of an application. Pin
Gerry Schmitz3-Feb-16 8:15
mveGerry Schmitz3-Feb-16 8:15 
AnswerRe: Write log from multiple instances of an application. Pin
Eddy Vluggen3-Feb-16 9:15
professionalEddy Vluggen3-Feb-16 9:15 
AnswerRe: Write log from multiple instances of an application. Pin
John Torjo3-Feb-16 13:47
professionalJohn Torjo3-Feb-16 13:47 
QuestionI want write process synchronization for .Net applictaion Pin
Member 110856472-Feb-16 19:49
Member 110856472-Feb-16 19:49 
AnswerRe: I want write process synchronization for .Net applictaion Pin
Dave Kreskowiak3-Feb-16 2:43
mveDave Kreskowiak3-Feb-16 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.