-
Notifications
You must be signed in to change notification settings - Fork 7
/
Example.cs
238 lines (196 loc) · 4.76 KB
/
Example.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibraryNamespace;
using LibraryNamespace;
using OurNamespace;
using OurNamespace;
namespace Mandatory.Namespace
{
/*
* Static members example
*/
public static class StaticExample
{
public const string Const2 = "Const2";
private const string Const1 = "Const1";
public static string publicStaticField;
private static string _privateStaticField;
static StaticExample()
{
PrivateStaticField = Const1 + Const1.GetHashCode();
}
public static string StaticProperty { get; set; }
public static string PublicStaticMethod(string argument) => PrivateStaticMethod(argument);
private static string PrivateStaticMethod(string argument) => argument + Const1;
}
public static class ExampleExtensions
{
public static T[] AsArray<T>(this T value) => new[] { value };
public static IEnumerable<string> CreateString<T>(this IEnumerable<T> enumerable)
=> enumerable.Select(val => val.ToString())
.Aggregate((sum, val) => sum + " " + val)
.AsArray();
}
/*
* Methods example
*/
public class MethodsExample
{
public string Name { get; set; }
public IEnumerator<T> YieldExample<T>(T[,] valuesGrid, (int x, int y) from, (int x, int y) to)
{
for (int i = from.x; i <= to.x; i++)
for (int j = from.y; j <= to.y; j++)
yield return valuesGrid[i, j];
}
public void ComplexMethod<T>(IEnumerable<T> arguments) where T : class
{
if (arguments == null)
throw new NullReferenceException();
var parser = new Parser();
var exceptions = new List<Exception>();
foreach (var value in arguments)
if (value != null)
{
StaticExample.PublicStaticMethod($"{this}.{nameof(LongMethod)} - {item}");
if (!ValidateExample(value, parser))
exceptions.Add(new Exception($"{value} - is not valid"));
}
else
throw new NullReferenceException("One of arguments is null");
if (exceptions.Any())
throw new AggregateException(exceptions);
}
private bool ValidateExample<T>(string value, ParserExample parser)
{
using (var stream = new Stream())
using (var reader = new StreamReader(stream))
{
try
{
parser.Parse<T>(reader);
return true;
}
catch (Exception e)
{
StaticExample.PublicStaticMethod(e.Message);
return false;
}
}
}
public void MultilineStatemenstExample()
{
var value = "line1"
+ "line2"
+ "line3";
var value = IsValue1
&& IsValue2
|| IsValue3;
var value = someObject.ChainedMethod1()
?.ChainedMethod2()
?.ChainedMethod3()
?? FallbackValue;
}
public void LongArgsMethod(
StructExample firstLongArgument,
EnumExample secondLongArgument,
FlagsExample thirdLongargument
)
{
EmptyMethod();
EmptyMethod();
}
public void LambdaMethod()
=> CallBackMethod(() => EmptyMethod());
public void LambdaMethod()
=> CallBackMethod(() =>
{
for (int i = 0; i <= 10; i++)
EmptyMethod();
});
public void CallBackMethod(Action action) => action();
public void EmptyMethod() { }
public override string ToString() => $"[{nameof(MethodsExample)}]{Name}";
public class ParserExample
{
public T Parse<T>(TextReader reader) => throw new NotImplementedException();
}
}
/*
* Complex type declaration example
*/
public class BaseClass { }
public interface ISomeInterface { }
public interface IAnotherInterface { }
public class ComplexType<TArg1, TArg2, TArg3> : BaseClass, ISomeInterface, IAnotherInterface
where TArg1 : IEnumerable<TArg2>
where TArg2 : class
where TArg3 : TArg2
{
}
/*
* MonoBehaviour example
*/
[RequireComponent(typeof(Rigidbody))]
public sealed class ComponentExample : MonoBehaviour
{
[Range(0, 10)]
[SerializeField]
private float _inspectorValue;
private Rigidbody _rigidbody;
public float InspectorValue
{
get => _inspectorValue;
set => _inspectorValue = value;
}
private void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
}
private void Start()
{
}
private void Update()
{
}
}
/*
* Struct example
*/
public struct StructExample
{
public int IntValue { get; private set; }
public string StringValue { get; private set; }
public StructExample(int intValue, string stringValue) : this()
=> (IntValue, StringValue) = (intValue, stringValue);
public static StructExample CreateStructExample(int intValue, string stringValue)
=> new StructExample()
{
IntValue = intValue,
StringValue = stringValue
};
}
/*
* Enums examples
*/
public enum EnumExample
{
ValueOne,
ValueTwo,
ValueThree
}
[Flags]
public enum FlagsExample
{
None = 0x0000,
Up = 0x1000,
Down = 0x0100,
Left = 0x0010,
Right = 0x0001,
Vertical = 0x1100,
Horizontal = 0x0011,
All = 0x1111
}
}