Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions csharp/CSharp/Assignments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Logging;

namespace CSharp;

public class Assignments(ILogger<Assignments> _logger)
{
public Data? NullableData { get; set; } = default;

public void NullConditional()
{
NullableData?.Value = "test";

_logger.LogInformation($"Before data is created, NullableData: {NullableData}");

NullableData = new();
NullableData?.Value = "test";

_logger.LogInformation($"After data is created, NullableData: {NullableData} - Value: {NullableData?.Value}");
}

public class Data
{
public string? Value { get; set; }
}
}
13 changes: 13 additions & 0 deletions csharp/CSharp/CollectionExpressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public void CollectionInitialization()
_logger.LogInformation($"Array initialization with spread element and members: {string.Join(',', anotherIntCollection)}");
}

public void Conversions()
{
int[] numbers = [1, 2, 3];

// T[] -> Span<T>
Span<int> spanNumbers = numbers;
_logger.LogInformation($"Implicit Conversion int[] -> span<int>: {string.Join(',', spanNumbers.ToArray())}");

// T[] -> ReadOnlySpan<T>
ReadOnlySpan<int> readOnlyNumbers = numbers;
_logger.LogInformation($"Implicit Conversion int[] -> ReadOnlySpan<int>: {string.Join(',', readOnlyNumbers.ToArray())}");
}

public void CallMethods()
{
SomeMethod("arg1", "arg2", "arg3");
Expand Down
12 changes: 11 additions & 1 deletion csharp/CSharp/LambdaParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CSharp;

public class LambdaParameters(ILogger<CollectionExpressions> _logger)
public class LambdaParameters(ILogger<LambdaParameters> _logger)
{
public void OptionalParameters()
{
Expand Down Expand Up @@ -32,4 +32,14 @@ void Params(params int[] values) { }
optional();
@params();
}

private delegate bool TryParse<T>(string text, out T result);

public void ParameterModifiersWithoutTypes()
{
// Lambdas support modifiers (ref/in/out/scoped/ref readonly) without explicit parameter types when the target delegate type is known
TryParse<int> parse = (text, out result) => int.TryParse(text, out result);

_logger.LogInformation($"(text, out result) => int.TryParse: ok={parse("42", out int value)}, result={value}");
}
}
12 changes: 11 additions & 1 deletion csharp/CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

var serviceCollection = new ServiceCollection();

serviceCollection.AddSingleton<Assignments>();
serviceCollection.AddSingleton<CollectionExpressions>();
serviceCollection.AddSingleton<EncodingDecoding>();
serviceCollection.AddSingleton<LambdaParameters>();
serviceCollection.AddSingleton<Params>();
serviceCollection.AddSingleton<PropertiesAndFields>();

serviceCollection.AddLogging(options =>
{
Expand All @@ -17,19 +19,27 @@

var serviceProvider = serviceCollection.BuildServiceProvider();

var assignments = serviceProvider.GetRequiredService<Assignments>();
var collectionExpressions = serviceProvider.GetRequiredService<CollectionExpressions>();
var encodingDecoding = serviceProvider.GetRequiredService<EncodingDecoding>();
var lambdaParameters = serviceProvider.GetRequiredService<LambdaParameters>();
var @params = serviceProvider.GetRequiredService<Params>();
var propertiesAndFields = serviceProvider.GetRequiredService<PropertiesAndFields>();

@params.Use();

assignments.NullConditional();

collectionExpressions.EmptyCollectionInitialization();
collectionExpressions.CollectionInitialization();
collectionExpressions.Conversions();
collectionExpressions.CallMethods();

lambdaParameters.OptionalParameters();
lambdaParameters.ParamsArrayParameters();
lambdaParameters.NewAcceptedBehavior();
lambdaParameters.ParameterModifiersWithoutTypes();

encodingDecoding.RunShowCases();

encodingDecoding.RunShowCases();
propertiesAndFields.FieldKeyword();
18 changes: 18 additions & 0 deletions csharp/CSharp/PropertiesAndFields.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.Extensions.Logging;

namespace CSharp;

public class PropertiesAndFields(ILogger<PropertiesAndFields> _logger)
{
string _filed = "field";

public string WithOutFieldKeyword { get => _filed; set => _filed = value; }
public string WithFieldKeyword { get; set => field = value.Trim(); } = "WithField";

public void FieldKeyword()
{
_logger.LogInformation($"_field => '{_filed}'");
_logger.LogInformation($"WithOutFieldKeyword => '{WithOutFieldKeyword}'");
_logger.LogInformation($"WithFieldKeyword => '{WithFieldKeyword}'");
}
}
2 changes: 2 additions & 0 deletions csharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ projects.

What we have learned and implemented so far are the following.

- Assignments
- Collection expressions
- Different Params type usage
- Encoding Decoding
- Lambda Parameters
- Properties and fields
Loading