Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7952143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:46:24+00:00 2026-06-04T02:46:24+00:00

Given the following program: using System; using System.Collections.Generic; namespace ConsoleApplication49 { using FooSpace; class

  • 0

Given the following program:

using System;
using System.Collections.Generic;

namespace ConsoleApplication49
{
    using FooSpace;

    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<FooBase> foos = FooFactory.CreateFoos();

            foreach (var foo in foos)
            {             
                HandleFoo(foo);
            }
        }

        private static void HandleFoo(FooBase foo)
        {
            dynamic fooObject = foo;
            ApplyFooDefaults(fooObject);
        }

        private static void ApplyFooDefaults(Foo1 foo1)
        {
            foo1.Name = "Foo 1";

            Console.WriteLine(foo1);
        }

        private static void ApplyFooDefaults(Foo2 foo2)
        {
            foo2.Name        = "Foo 2";
            foo2.Description = "SomeDefaultDescription";

            Console.WriteLine(foo2);
        }

        private static void ApplyFooDefaults(Foo3 foo3)
        {
            foo3.Name    = "Foo 3";
            foo3.MaxSize = Int32.MaxValue;

            Console.WriteLine(foo3);
        }

        private static void ApplyFooDefaults(Foo4 foo4)
        {
            foo4.Name        = "Foo 4";
            foo4.MaxSize     = 99999999;
            foo4.EnableCache = true;

            Console.WriteLine(foo4);
        }

        private static void ApplyFooDefaults(FooBase unhandledFoo)
        {
            unhandledFoo.Name = "Unhandled Foo";
            Console.WriteLine(unhandledFoo);
        }
    }    
}

/////////////////////////////////////////////////////////
// Assume this namespace comes from a different assembly
namespace FooSpace
{
    ////////////////////////////////////////////////
    // these cannot be changed, assume these are 
    // from the .Net framework or some 3rd party
    // vendor outside of your ability to alter, in
    // another assembly with the only way to create
    // the objects is via the FooFactory and you
    // don't know which foos are going to be created
    // due to configuration.

    public static class FooFactory
    {
        public static IEnumerable<FooBase> CreateFoos()
        {
            List<FooBase> foos = new List<FooBase>();
            foos.Add(new Foo1());
            foos.Add(new Foo2());
            foos.Add(new Foo3());
            foos.Add(new Foo4());
            foos.Add(new Foo5());

            return foos;
        }
    }

    public class FooBase
    {
        protected FooBase() { }

        public string Name { get; set; }

        public override string ToString()
        {
            return String.Format("Type = {0}, Name=\"{1}\"", this.GetType().FullName, this.Name);
        }
    }

    public sealed class Foo1 : FooBase
    {
        internal Foo1() { }
    }

    public sealed class Foo2 : FooBase
    {
        internal Foo2() { }

        public string Description { get; set; }

        public override string ToString()
        {
            string baseString =  base.ToString();
            return String.Format("{0}, Description=\"{1}\"", baseString, this.Description);
        }
    }

    public sealed class Foo3 : FooBase
    {
        internal Foo3() { }

        public int MaxSize { get; set; }

        public override string ToString()
        {
            string baseString =  base.ToString();
            return String.Format("{0}, MaxSize={1}", baseString, this.MaxSize);
        }
    }

    public sealed class Foo4 : FooBase
    {
        internal Foo4() { }

        public int MaxSize { get; set; }
        public bool EnableCache { get; set; }

        public override string ToString()
        {
            string baseString =  base.ToString();
            return String.Format("{0}, MaxSize={1}, EnableCache={2}", baseString,
                                                                      this.MaxSize,
                                                                      this.EnableCache);
        }
    }

    public sealed class Foo5 : FooBase
    {
        internal Foo5() { }
    }
    ////////////////////////////////////////////////
}

Which produces the following output:

Type = ConsoleApplication49.Foo1, Name="Foo 1"
Type = ConsoleApplication49.Foo2, Name="Foo 2", Description="SomeDefaultDescription"
Type = ConsoleApplication49.Foo3, Name="Foo 3", MaxSize=2147483647
Type = ConsoleApplication49.Foo4, Name="Foo 4", MaxSize=99999999, EnableCache=True
Type = ConsoleApplication49.Foo5, Name="Unhandled Foo"
Press any key to continue . . .

I chose to use dynamic here to avoid the following:

  1. using switch/if/else statements e.g. switch(foo.GetType().Name)
  2. explicit type checking statements e.g. foo is Foo1
  3. explicit casting statements e.g. (Foo1)foo

Because of the dynamic conversion, the correct ApplyFooDefaults method gets invoked based on the type of the object passed into HandleFoo(FooBase foo). Any objects that do not have an appropriate ApplyFooDefaults handler method, fall into the “catch all” method, ApplyFooDefaults(FooBase unhandledFoo).

One key part here is that FooBase and the derived classes represent types that are outside of our control and cannot be derived from to add additional interfaces.

Is this a “good” use for dynamic or can this problem be solved in an OOP way without adding additional complexity given the constraints and the fact that this is just to set default property values on these objects?

*UPDATED*

After Bob Horn’s answer, I realized that my scenario wasn’t complete. Additional constraints:

  1. You can’t create the Foos directly, you have to use the FooFactory.
  2. You can’t assume the Foo type because the Foo type is specified in
    configuration and created reflectively.

.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:46:25+00:00Added an answer on June 4, 2026 at 2:46 am

    Well, individual object initialization should happen in the constructors of the types.
    If the factory fails to do that and outputs an object with a base type only then clearly it is beyond OOP patterns to initialize the objects based on type.

    Alas, runtime type detection is the way to go and dynamic just does that, so yes, your solution is very pretty. (but the third party lib is not, because it forces you to use dynamic types)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code: using System.Collections.Generic; static class Program { static void Main() {
Consider the following C# program: using System; using System.Diagnostics; namespace Test { class MainClass
Consider the following code: using System; using System.Runtime.InteropServices; namespace Demo { class Program {
Given the following example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization.Formatters.Binary;
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Given the following code snippet: using System; using Foo = System.Int32; namespace ConsoleApplication3 {
Given the following simple program: import wx class TestDraw(wx.Panel): def __init__(self,parent=None,id=-1): wx.Panel.__init__(self,parent,id,style=wx.TAB_TRAVERSAL) self.SetBackgroundColour(#FFFFFF) self.Bind(wx.EVT_PAINT,self.onPaint)
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Given the following two ways of defining a class method in Ruby: class Foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.