Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GenericClass<T> where T : new() {
- //class definition
- private void SomeMethod() {
- T myT = new T();
- //work with myT
- }
- }
- public class foo
- {
- public string bar = "abc";
- }
- public class Container<T>
- {
- T MaybeFoo = default;
- }
- a = new Container<foo>();
- // a.MaybeFoo is null;
- public class foo
- {
- public string bar = "abc";
- }
- public class Container<T> where T : new()
- {
- T MaybeFoo = new T();
- }
- a = new Container<foo>();
- Console.WriteLine( ((Foo)a.MaybeFoo).bar ) // prints "abc"
Add Comment
Please, Sign In to add comment