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

  • SEARCH
  • Home
  • 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 7786803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:28:58+00:00 2026-06-01T20:28:58+00:00

interface IExecutor { void Execute(); } class Executor2<T> where T : IExecutor { public

  • 0
interface IExecutor
{
   void Execute();
}

class Executor2<T> where T : IExecutor
{
    public void Execute()
    {
        var ex = (T)Activator.CreateInstance(typeof(T));
        ex.Execute();
    }
}

It was a question at an interview. They told me that sometimes this code falls (causes exceptions) and there are at least 3 reasons that could cause the problems. It’s unknown what exceptions were there. But the method Execute was created good, its implementation has no fault.

Does anybody have a suggestion about that?

Edit: There are at least 3 reasons that could cause the problems. What are these reasons?

  • 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-01T20:29:00+00:00Added an answer on June 1, 2026 at 8:29 pm

    On the face of it I can see a few issues.

    1. The code doesn’t compile, but I can ignore that and make it compile.
    2. The code doesn’t do what you think it does.

    To explain 2: you specify a type T at the class definition with a constraint on IExecutor, but you then define another type T at the method level without a constraint. This doesn’t compile.

    If I fix this and remove the <T> definition from the method I can see a number of reasons for it failing without much warning:

    1. ex is null.
    2. Type T doesn’t have a public parameterless constructor defined.
    3. Perhaps it cannot load the DLL containing T.

    As spotted by Jakub:

    1. T could be an interface (no constructor).
    2. T could be an abstract class, these don’t allow instances to be created directly.

    The first can be guarded against using a null check if (ex != null) and the second can be guarded against using another generic constraint new():

    class Executor2<T> where T : IExecutor, new()
    {
    }
    

    Obviously you could also amend your code to including exception logging. This might be useful in figuring out what the actual problems are instead of just stabbing in the dark:

    public void Execute<T>()
    {
        try
        {
            var ex = (T)Activator.CreateInstance(typeof(T));
            ex.Execute();
        }
        catch (Exception ex)
        {
            Log(ex); // Mystical logging framework.
            throw;
        }
    }
    

    This is the only answer I can cobble together considering I didn’t understand the question.

    If I were asked this in an interview I’d likely say I couldn’t name all 3, but I’d know how to change the code to be more maintainable and tell me what was wrong. Then I’d likely walk for asking pointless interview questions.

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

Sidebar

Related Questions

interface A { void hi(); } class AImpl implements A { public void hi()
interface IVehicle { void DoSth(); } class VW : IVehicle { public virtual void
interface Receiver { public <T extends HasId> void doSomethingWithList(List<T> list); } also, class SubClass
interface Device { public void doIt(); } public class Electronic implements Device { public
interface IXXX { void Foo(); } class XXX : IXXX { public static void
interface I { int J(); } class A : I { public int J(){return
interface ICanvasTool { void Motion(Point newLocation); void Tick(); } abstract class CanvasTool_BaseDraw : ICanvasTool
interface ILol { void LOL(); } class Rofl : ILol { void ILol.LOL() {
public interface IMy<T> { T Implementer { get; } } public class MyClass :
interface ICloneable<out T> { T Clone(); } class Base : ICloneable<Base> { public Base

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.