Generics.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ?// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. public static class Generics
  21. {
  22. public class MyArray<T>
  23. {
  24. public class NestedClass<Y>
  25. {
  26. public T Item1;
  27. public Y Item2;
  28. }
  29. public enum NestedEnum
  30. {
  31. A,
  32. B
  33. }
  34. private T[] arr;
  35. public MyArray(int capacity)
  36. {
  37. this.arr = new T[capacity];
  38. }
  39. public void Size(int capacity)
  40. {
  41. Array.Resize<T>(ref this.arr, capacity);
  42. }
  43. public void Grow(int capacity)
  44. {
  45. if (capacity >= this.arr.Length)
  46. {
  47. this.Size(capacity);
  48. }
  49. }
  50. }
  51. public interface IInterface
  52. {
  53. void Method1<T>() where T : class;
  54. void Method2<T>() where T : class;
  55. }
  56. public abstract class Base : Generics.IInterface
  57. {
  58. // constraints must be repeated on implicit interface implementation
  59. public abstract void Method1<T>() where T : class;
  60. // constraints must not be specified on explicit interface implementation
  61. void Generics.IInterface.Method2<T>()
  62. {
  63. }
  64. }
  65. public class Derived : Generics.Base
  66. {
  67. // constraints are inherited automatically and must not be specified
  68. public override void Method1<T>()
  69. {
  70. }
  71. }
  72. private const Generics.MyArray<string>.NestedEnum enumVal = Generics.MyArray<string>.NestedEnum.A;
  73. private static Type type1 = typeof(List<>);
  74. private static Type type2 = typeof(Generics.MyArray<>);
  75. private static Type type3 = typeof(List<>.Enumerator);
  76. private static Type type4 = typeof(Generics.MyArray<>.NestedClass<>);
  77. private static Type type5 = typeof(List<int>[]);
  78. private static Type type6 = typeof(Generics.MyArray<>.NestedEnum);
  79. public static void MethodWithConstraint<T, S>() where T : class, S where S : ICloneable, new()
  80. {
  81. }
  82. public static void MethodWithStructConstraint<T>() where T : struct
  83. {
  84. }
  85. public static Dictionary<string, string>.KeyCollection.Enumerator GetEnumerator(Dictionary<string, string> d, Generics.MyArray<string>.NestedClass<int> nc)
  86. {
  87. // Tests references to inner classes in generic classes
  88. return d.Keys.GetEnumerator();
  89. }
  90. }