Skip to content

Commit

Permalink
Generics review the code and UT - working copy
Browse files Browse the repository at this point in the history
- added markdown with notes
- refactoring text
  • Loading branch information
mpostol committed May 21, 2018
1 parent 76c3e74 commit a40abbc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Lecture/Lecture/Generics.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;

using System;
using System.Collections.Generic;

namespace TP.Lecture
Expand Down
18 changes: 18 additions & 0 deletions Lecture/Lecture/Generics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generics notes

Constraints are specified by using the where contextual keyword. The following table lists the seven types of constraints:

|Constraint |Description|
|-|-|
|where T: struct| The type argument must be a value type. Any value type except Nullable can be specified. For more information, see Using Nullable Types.|
|where T : class| The type argument must be a reference type. This constraint applies also to any class, interface, delegate, or array type.|
|where T : unmanaged| The type argument must not be a reference type and must not contain any reference type members at any level of nesting.|
|where T : new()| The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last.|
|where T : <base class name>| The type argument must be or derive from the specified base class.|
|where T : <interface name>|The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic.|
|where T : U| The type argument supplied for T must be or derive from the argument supplied for U.|

Some of the constraints are mutually exclusive. All value types must have an accessible parameterless constructor. The struct constraint implies the new() constraint and the new() constraint cannot be combined with the struct constraint. The unmanaged constraint implies the struct constraint. The unmanaged constraint cannot be combined with either the struct or new() constraints.

# See also
[Constraints on type parameters](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters)
1 change: 1 addition & 0 deletions Lecture/Lecture/Lecture.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Generics.md" />
</ItemGroup>
<ItemGroup>
<Page Include="TreeViewExample\MainWindow.xaml">
Expand Down
4 changes: 3 additions & 1 deletion Lecture/Lesson2UnitTest/GenericsUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public void DictionaryNotImplementedExceptionTestMethod()
Assert.AreEqual<int>(2, _dictionary.Count);
_dictionary.ContainsKey(_EquatableNotImplementedInstance);
}
private class AnyClass{}
#region UT instrumentation
private class AnyClass { }
private class EquatableNotImplemented : System.IEquatable<EquatableNotImplemented>
{
public bool Equals(EquatableNotImplemented other)
Expand All @@ -46,5 +47,6 @@ public bool Equals(EquatableNotImplemented other)
}
}

#endregion
}
}

1 comment on commit a40abbc

@mpostol
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It relates to: Generics review the code and UT #79

Please sign in to comment.