Skip to content

Commit 6045b51

Browse files
author
ahotko
committed
Added Samples for:
- Conditionals - Anonymous Types - Caller Info - Extension Methods - (Is) Null Minor refactoring
1 parent a1e67ac commit 6045b51

File tree

9 files changed

+252
-58
lines changed

9 files changed

+252
-58
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#define CONDITION1
2+
#define CONDITION2
3+
4+
using System;
5+
using System.Diagnostics;
6+
7+
namespace CodeSamples.Attributes
8+
{
9+
public class ConditionalSample : SampleExecute
10+
{
11+
[Conditional("CONDITION1")]
12+
private void MethodCondition1()
13+
{
14+
Console.WriteLine("MethodCondition1");
15+
}
16+
17+
[Conditional("CONDITION2")]
18+
private void MethodCondition2()
19+
{
20+
Console.WriteLine("MethodCondition2");
21+
}
22+
23+
[Conditional("CONDITION3")]
24+
private void MethodCondition3()
25+
{
26+
Console.WriteLine("MethodCondition3");
27+
}
28+
29+
[Conditional("CONDITION1"), Conditional("CONDITION2")]
30+
private void MethodCondition1or2()
31+
{
32+
Console.WriteLine("MethodCondition1or2");
33+
}
34+
35+
[Conditional("CONDITION1"), Conditional("CONDITION3")]
36+
private void MethodCondition1or3()
37+
{
38+
Console.WriteLine("MethodCondition1or3");
39+
}
40+
41+
public override void Execute()
42+
{
43+
Title("ConditionalSampleExecute");
44+
MethodCondition1();
45+
MethodCondition2();
46+
MethodCondition3();
47+
MethodCondition1or2();
48+
MethodCondition1or3();
49+
Finish();
50+
}
51+
}
52+
}

CSharp Code Samples/CodeSamples/Attributes/DebuggingSample.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@ public FullName(string firstName, string lastName)
1919
public string LastName { get; set; }
2020
}
2121

22+
/// <summary>
23+
/// <para>
24+
/// DebuggerBrowsableState.Collapsed - This signifies that the default behaviour should be used for the decorated member and
25+
/// gives the equivalent results as when the attribute is omitted. When viewed in a debugging tool the member is visible
26+
/// and can be expanded to allow access to any further members that it contains.
27+
/// </para>
28+
/// <para>
29+
/// DebuggerBrowsableState.Never - This indicates that the member should not be displayed in debugging windows. The member is
30+
/// hidden from view completely.
31+
/// </para>
32+
/// <para>
33+
/// DebuggerBrowsableState.RootHidden - This signifies that the member should not be visible but that its own members should be.
34+
/// The members of the hidden item appear as if they were one level higher in the hierarchy of values. This setting is useful
35+
/// for members that are used only to store structured information, such as collection types or some data objects.
36+
/// </para>
37+
/// </summary>
2238
sealed class DebuggerExamplesDebuggerBrowsable
2339
{
24-
/// <summary>
25-
/// DebuggerBrowsableState.Collapsed - This signifies that the default behaviour should be used for the decorated member and
26-
/// gives the equivalent results as when the attribute is omitted. When viewed in a debugging tool the member is visible
27-
/// and can be expanded to allow access to any further members that it contains.
28-
/// DebuggerBrowsableState.Never - This indicates that the member should not be displayed in debugging windows. The member is
29-
/// hidden from view completely.
30-
/// DebuggerBrowsableState.RootHidden - This signifies that the member should not be visible but that its own members should be.
31-
/// The members of the hidden item appear as if they were one level higher in the hierarchy of values. This setting is useful
32-
/// for members that are used only to store structured information, such as collection types or some data objects.
33-
/// </summary>
34-
3540
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
3641
public string StandardProperty { get; set; }
3742

@@ -51,22 +56,28 @@ public int GetIntValue()
5156
return 5*2;
5257
}
5358

59+
/// <summary>
60+
/// Also see <see cref="ConditionalSample"/> class
61+
/// </summary>
5462
[Conditional("DEBUG")]
5563
public void ExecuteOnlyInDebugMode()
5664
{
57-
Console.WriteLine($"Executed in DEBUG mode");
65+
Console.WriteLine("Executed in DEBUG mode");
5866
}
5967

68+
/// <summary>
69+
/// Also see <see cref="ConditionalSample"/> class
70+
/// </summary>
6071
[Conditional("RELEASE")]
6172
public void ExecuteOnlyInReleaseMode()
6273
{
63-
Console.WriteLine($"Executed in RELEASE mode");
74+
Console.WriteLine("Executed in RELEASE mode");
6475
}
6576

6677
#if DEBUG
6778
public void CompileOnlyWhenDebugDefined()
6879
{
69-
Console.WriteLine($"Compiled and Executed in DEBUG mode");
80+
Console.WriteLine("Compiled and Executed in DEBUG mode");
7081
}
7182
#endif
7283

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace CodeSamples.Classes
4+
{
5+
public class AnonymousTypesSample : SampleExecute
6+
{
7+
/// <summary>
8+
/// Creates anonymous type (object) with properties Name, Lastname and Age
9+
/// </summary>
10+
public override void Execute()
11+
{
12+
Title("AnonymousTypesSampleExecute");
13+
var anonymousObject = new { Name = "Foo", Lastname = "Bar", Age = 13 };
14+
Console.WriteLine($"anonymousObject(), Name = {anonymousObject.Name}, Lastname = {anonymousObject.Lastname}, Age = {anonymousObject.Age}");
15+
Finish();
16+
}
17+
}
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace CodeSamples.Classes
4+
{
5+
public class CallerInfoSample : SampleExecute
6+
{
7+
public void TraceMessage(string message,
8+
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
9+
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
10+
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
11+
{
12+
Console.WriteLine($"Message = {message}");
13+
Console.WriteLine($"Member (Method) Name = {memberName}");
14+
Console.WriteLine($"Source File Path = {sourceFilePath}");
15+
Console.WriteLine($"Line Number = {sourceLineNumber}");
16+
}
17+
18+
public override void Execute()
19+
{
20+
Title("CallerInfoSampleExecute");
21+
TraceMessage("Tracing this call...");
22+
Finish();
23+
}
24+
}
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace CodeSamples.Classes
5+
{
6+
public static class ExtensionMethods
7+
{
8+
public static string PadRightWithString(this string value, string padding, int paddingCount)
9+
{
10+
var result = new StringBuilder(value);
11+
for (int i = 0; i < paddingCount; i++)
12+
{
13+
result.Append(padding);
14+
}
15+
return result.ToString();
16+
}
17+
18+
public static void WriteToConsole(this string value)
19+
{
20+
Console.WriteLine($"Contents Of String = {value}");
21+
}
22+
}
23+
24+
public class ExtensionMethodsSample : SampleExecute
25+
{
26+
public override void Execute()
27+
{
28+
Title("ExtensionMethodsSampleExecute");
29+
string testString = "test string";
30+
string resultString = testString.PadRightWithString("<pad>", 5);
31+
Console.WriteLine("Original string:");
32+
testString.WriteToConsole();
33+
Console.WriteLine("Padded string:");
34+
resultString.WriteToConsole();
35+
Finish();
36+
}
37+
}
38+
}

CSharp Code Samples/CodeSamples/CodeSamples.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@
5252
<ItemGroup>
5353
<Compile Include="Alterations\EntityConversionSample.cs" />
5454
<Compile Include="Alterations\OperatorOverloadingSample.cs" />
55+
<Compile Include="Attributes\ConditionalSample.cs" />
5556
<Compile Include="Attributes\ObsoleteSample.cs" />
57+
<Compile Include="Classes\AnonymousTypesSample.cs" />
58+
<Compile Include="Classes\CallerInfoSample.cs" />
5659
<Compile Include="Classes\ConstructorChainingSample.cs" />
5760
<Compile Include="Attributes\DebuggingSample.cs" />
61+
<Compile Include="Classes\ExtensionMethodsSample.cs" />
5862
<Compile Include="ISampleExecute.cs" />
5963
<Compile Include="MultiThreading\BackgroundWorkerSample.cs" />
6064
<Compile Include="Patterns\Behavioral\ObserverPattern.cs" />
@@ -69,6 +73,7 @@
6973
<Compile Include="Program.cs" />
7074
<Compile Include="Properties\AssemblyInfo.cs" />
7175
<Compile Include="SampleExecute.cs" />
76+
<Compile Include="SyntacticSugars\NullSample.cs" />
7277
<Compile Include="SyntacticSugars\PatternMatchingSample.cs" />
7378
<Compile Include="SyntacticSugars\PropertiesSample.cs" />
7479
<Compile Include="SyntacticSugars\StringInterpolationSample.cs" />

CSharp Code Samples/CodeSamples/Program.cs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,52 +39,67 @@ static void Main(string[] args)
3939
var props = new PropertiesSample();
4040
props.Execute();
4141

42-
var usings = new UsingSample();
43-
usings.Execute();
42+
var Sample = new UsingSample();
43+
Sample.Execute();
4444

45-
var interpol = new StringInterpolationSample();
46-
interpol.Execute();
45+
var stringInterpolation = new StringInterpolationSample();
46+
stringInterpolation.Execute();
4747

48-
var patterns = new PatternMatchingSample();
49-
patterns.Execute();
48+
var patternMatchingSample = new PatternMatchingSample();
49+
patternMatchingSample.Execute();
50+
51+
var nullSample = new NullSample();
52+
nullSample.Execute();
5053
#endregion
5154

5255
#region Useful
53-
var useful = new ClassAndMethodNamesSample();
54-
useful.Execute();
56+
var classAndMethodNamesSample = new ClassAndMethodNamesSample();
57+
classAndMethodNamesSample.Execute();
58+
59+
var callerInfo = new CallerInfoSample();
60+
callerInfo.Execute();
5561
#endregion
5662

5763
#region Patterns
58-
var patts = new PatternsSample();
59-
patts.Execute();
64+
var patternsSample = new PatternsSample();
65+
patternsSample.Execute();
6066
#endregion
6167

6268
#region LINQ
63-
var linqs = new LinqSample();
64-
linqs.Execute();
69+
var linqSample = new LinqSample();
70+
linqSample.Execute();
6571
#endregion
6672

6773
#region Operators
68-
var opera = new OperatorOverloadingSample();
69-
opera.Execute();
74+
var operatorOverloadingSample = new OperatorOverloadingSample();
75+
operatorOverloadingSample.Execute();
7076
#endregion
7177

7278
#region Entity Conversion
73-
var entityConv = new EntityConversionSample();
74-
entityConv.Execute();
79+
var entityConversionSample = new EntityConversionSample();
80+
entityConversionSample.Execute();
7581
#endregion
7682

7783
#region Classes
78-
var consChain = new ConstructorChainingSample();
79-
consChain.Execute();
84+
var constructorChainingSample = new ConstructorChainingSample();
85+
constructorChainingSample.Execute();
86+
87+
var anonymousTypesSample = new AnonymousTypesSample();
88+
anonymousTypesSample.Execute();
89+
90+
var extensionMethodsSample = new ExtensionMethodsSample();
91+
extensionMethodsSample.Execute();
8092
#endregion
8193

8294
#region Attributes
8395
var debuggerAttributeSample = new DebuggingSample();
8496
debuggerAttributeSample.Execute();
8597

86-
var oboleteAttributeSample = new ObsoleteSample();
87-
oboleteAttributeSample.Execute();
98+
var obsoleteAttributeSample = new ObsoleteSample();
99+
obsoleteAttributeSample.Execute();
100+
101+
var conditionalAttributeSample = new ConditionalSample();
102+
conditionalAttributeSample.Execute();
88103
#endregion
89104

90105
#region Threading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace CodeSamples.SyntacticSugars
4+
{
5+
public class NullSample : SampleExecute
6+
{
7+
public override void Execute()
8+
{
9+
Title("NullSampleExecute");
10+
11+
Object obj = null;
12+
13+
if(obj is null)
14+
{
15+
Console.WriteLine("Object is null.");
16+
}
17+
18+
if (obj is object)
19+
{
20+
Console.WriteLine("Object is not null.");
21+
}
22+
23+
Finish();
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)