C#: Creating an instance of an abstract class without defining new class

前端 未结 9 811
眼角桃花
眼角桃花 2020-12-29 04:25

I know it can be done in Java, as I have used this technique quite extensively in the past. An example in Java would be shown below. (Additional question. What is this techn

9条回答
  •  醉酒成梦
    2020-12-29 05:15

    That can't be done in C#; you need to declare a new class type. The closest you can get in C# is probably a named nested class:

    public class StartHere{
        private class Foo : Example {
            public override void  doStuff()
            {
                Console.WriteLine("did stuff");
            }
        }
       public static void Main(string[] args){
          Example x = new Foo();
          x.doStuff();
       }
    }
    

提交回复
热议问题