Guest User

Untitled

a guest
Aug 15th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. Create a object in C# without the use of new Keyword? [closed]
  2. Type type = typeof(MyClass);
  3. object[] parameters = new object[]{1, "hello", "world" };
  4. object obj = Activator.CreateInstance(type, parameters);
  5. MyClass myClass = obj as MyClass;
  6.  
  7. static class Foo
  8. {
  9. public static void Bar()
  10. {
  11. Console.WriteLine("Foo.Bar()");
  12. }
  13. }
  14.  
  15. static class PizzaFactory
  16. {
  17. static Pizza CreatePizza(String topping)
  18. {
  19. if (topping == "cheese")
  20. {
  21. return new CheesePizza();
  22. }
  23. else if (topping == "salami")
  24. {
  25. return new SalamiPizza();
  26. }
  27. }
  28. }
  29.  
  30. class Pizza { }
  31. class CheesePizza : Pizza { }
  32. class SalamiPizza : Pizza { }
  33.  
  34. var s = default(string); // null
  35. var i = default(int); // integer (0)
  36.  
  37. typeof(MyType)
Add Comment
Please, Sign In to add comment