-
Notifications
You must be signed in to change notification settings - Fork 37
/
ExtensionMethods.cs
76 lines (70 loc) · 3.23 KB
/
ExtensionMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//____________________________________________________________________________________________________________________________________
//
// Copyright (C) 2024, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community by pressing the `Watch` button and get started commenting using the discussion panel at
//
// https://github.com/mpostol/TP/discussions/182
//
// by introducing yourself and telling us what you do with this community.
//_____________________________________________________________________________________________________________________________________
using System;
namespace TP.FunctionalProgramming
{
/// <summary>
/// Define an example interface named IMyInterface.
/// </summary>
public interface IMyInterface
{
/// <summary>
/// Any class that implements IMyInterface must implement the method that matches the following signature.
/// </summary>
void MyInterfaceMethod();
}
/// <summary>
/// Class ExtensionMethods - defines a few extension methods.
/// </summary>
public static class ExtensionMethods
{
/// <summary>
/// Counts words in <paramref name="str"/>.
/// </summary>
/// <param name="str">The string to be analyzed.</param>
/// <returns>Number of word in the string as <see cref="System.Int32"/>.</returns>
public static int WordCount(this string str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
/// <summary>
/// Determines if the <paramref name="i"/> is even number.
/// </summary>
/// <param name="i">The value to be processed.</param>
/// <returns><c>true</c> if <paramref name="i"/> is even number, <c>false</c> otherwise.</returns>
public static bool Even(this int i)
{
return i % 2 == 0;
}
/// <summary>
/// Determines whether the specified <paramref name="value"/> object occurs within the <paramref name="sourceString" />.
/// </summary>
/// <param name="sourceString">The source string.</param>
/// <param name="value">The string to seek.</param>
/// <returns><c>true</c> if the value parameter occurs within this <paramref name="value" />, or if the <paramref name="value" /> is the empty string (""); otherwise, <c>false</c>.</returns>
/// <exception cref="System.NotImplementedException">An extension method with the same name and signature as an interface or class method will never be called</exception>
public static bool Contains(this string sourceString, string value)
{
throw new NotImplementedException("An extension method with the same name and signature as an interface or class method will never be called");
}
/// <summary>
/// Allows calling the <see cref="IMyInterface.MyInterfaceMethod"/> method against the null reference.
/// </summary>
/// <param name="myInterface">An instance of the <see cref="IMyInterface"/>.</param>
/// <exception cref="ArgumentNullException"> - it the <paramref name="myInterface"/> is null.</exception>
public static void ProtectedMyInterfaceMethodCall(this IMyInterface myInterface)
{
if (myInterface == null)
throw new ArgumentNullException($"{nameof(myInterface)} cannot be null.");
myInterface.MyInterfaceMethod();
}
}
}