Skip to content

Commit cfc7865

Browse files
committed
Enabled C# 9, switched to target typed new()
1 parent d437373 commit cfc7865

File tree

13 files changed

+42
-52
lines changed

13 files changed

+42
-52
lines changed

Microsoft.Toolkit.Mvvm/ComponentModel/ObservableObject.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ protected bool SetPropertyAndNotifyOnCompletion(ref TaskNotifier? taskNotifier,
340340
// instance. This will result in no further allocations after the first time this method is called for a given
341341
// generic type. We only pay the cost of the virtual call to the delegate, but this is not performance critical
342342
// code and that overhead would still be much lower than the rest of the method anyway, so that's fine.
343-
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier(), newValue, _ => { }, propertyName);
343+
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new(), newValue, _ => { }, propertyName);
344344
}
345345

346346
/// <summary>
@@ -362,7 +362,7 @@ protected bool SetPropertyAndNotifyOnCompletion(ref TaskNotifier? taskNotifier,
362362
/// </remarks>
363363
protected bool SetPropertyAndNotifyOnCompletion(ref TaskNotifier? taskNotifier, Task? newValue, Action<Task?> callback, [CallerMemberName] string? propertyName = null)
364364
{
365-
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier(), newValue, callback, propertyName);
365+
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new(), newValue, callback, propertyName);
366366
}
367367

368368
/// <summary>
@@ -401,7 +401,7 @@ protected bool SetPropertyAndNotifyOnCompletion(ref TaskNotifier? taskNotifier,
401401
/// </remarks>
402402
protected bool SetPropertyAndNotifyOnCompletion<T>(ref TaskNotifier<T>? taskNotifier, Task<T>? newValue, [CallerMemberName] string? propertyName = null)
403403
{
404-
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier<T>(), newValue, _ => { }, propertyName);
404+
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new(), newValue, _ => { }, propertyName);
405405
}
406406

407407
/// <summary>
@@ -424,7 +424,7 @@ protected bool SetPropertyAndNotifyOnCompletion<T>(ref TaskNotifier<T>? taskNoti
424424
/// </remarks>
425425
protected bool SetPropertyAndNotifyOnCompletion<T>(ref TaskNotifier<T>? taskNotifier, Task<T>? newValue, Action<Task<T>?> callback, [CallerMemberName] string? propertyName = null)
426426
{
427-
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier<T>(), newValue, callback, propertyName);
427+
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new(), newValue, callback, propertyName);
428428
}
429429

430430
/// <summary>

Microsoft.Toolkit.Mvvm/ComponentModel/ObservableRecipient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected virtual void OnDeactivated()
116116
/// </remarks>
117117
protected virtual void Broadcast<T>(T oldValue, T newValue, string? propertyName)
118118
{
119-
var message = new PropertyChangedMessage<T>(this, propertyName, oldValue, newValue);
119+
PropertyChangedMessage<T> message = new(this, propertyName, oldValue, newValue);
120120

121121
Messenger.Send(message);
122122
}

Microsoft.Toolkit.Mvvm/ComponentModel/ObservableValidator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public abstract class ObservableValidator : ObservableObject, INotifyDataErrorIn
2222
/// <summary>
2323
/// The cached <see cref="PropertyChangedEventArgs"/> for <see cref="HasErrors"/>.
2424
/// </summary>
25-
private static readonly PropertyChangedEventArgs HasErrorsChangedEventArgs = new PropertyChangedEventArgs(nameof(HasErrors));
25+
private static readonly PropertyChangedEventArgs HasErrorsChangedEventArgs = new(nameof(HasErrors));
2626

2727
/// <summary>
2828
/// The <see cref="Dictionary{TKey,TValue}"/> instance used to store previous validation results.
2929
/// </summary>
30-
private readonly Dictionary<string, List<ValidationResult>> errors = new Dictionary<string, List<ValidationResult>>();
30+
private readonly Dictionary<string, List<ValidationResult>> errors = new();
3131

3232
/// <summary>
3333
/// Indicates the total number of properties with errors (not total errors).
@@ -380,7 +380,7 @@ private void ValidateProperty(object? value, string? propertyName)
380380
// If the property isn't present in the dictionary, add it now to avoid allocations.
381381
if (!this.errors.TryGetValue(propertyName!, out List<ValidationResult>? propertyErrors))
382382
{
383-
propertyErrors = new List<ValidationResult>();
383+
propertyErrors = new();
384384

385385
this.errors.Add(propertyName!, propertyErrors);
386386
}
@@ -457,14 +457,14 @@ private bool TryValidateProperty(object? value, string? propertyName, out IReadO
457457
// Add the cached errors list for later use.
458458
if (!this.errors.TryGetValue(propertyName!, out List<ValidationResult>? propertyErrors))
459459
{
460-
propertyErrors = new List<ValidationResult>();
460+
propertyErrors = new();
461461

462462
this.errors.Add(propertyName!, propertyErrors);
463463
}
464464

465465
bool hasErrors = propertyErrors.Count > 0;
466466

467-
List<ValidationResult> localErrors = new List<ValidationResult>();
467+
List<ValidationResult> localErrors = new();
468468

469469
// Validate the property, by adding new errors to the local list
470470
bool isValid = Validator.TryValidateProperty(

Microsoft.Toolkit.Mvvm/DependencyInjection/Ioc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public sealed class Ioc : IServiceProvider
4747
/// <summary>
4848
/// Gets the default <see cref="Ioc"/> instance.
4949
/// </summary>
50-
public static Ioc Default { get; } = new Ioc();
50+
public static Ioc Default { get; } = new();
5151

5252
/// <summary>
5353
/// The <see cref="IServiceProvider"/> instance to use, if initialized.

Microsoft.Toolkit.Mvvm/Input/AsyncRelayCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ public sealed class AsyncRelayCommand : ObservableObject, IAsyncRelayCommand
2222
/// <summary>
2323
/// The cached <see cref="PropertyChangedEventArgs"/> for <see cref="CanBeCanceled"/>.
2424
/// </summary>
25-
internal static readonly PropertyChangedEventArgs CanBeCanceledChangedEventArgs = new PropertyChangedEventArgs(nameof(CanBeCanceled));
25+
internal static readonly PropertyChangedEventArgs CanBeCanceledChangedEventArgs = new(nameof(CanBeCanceled));
2626

2727
/// <summary>
2828
/// The cached <see cref="PropertyChangedEventArgs"/> for <see cref="IsCancellationRequested"/>.
2929
/// </summary>
30-
internal static readonly PropertyChangedEventArgs IsCancellationRequestedChangedEventArgs = new PropertyChangedEventArgs(nameof(IsCancellationRequested));
30+
internal static readonly PropertyChangedEventArgs IsCancellationRequestedChangedEventArgs = new(nameof(IsCancellationRequested));
3131

3232
/// <summary>
3333
/// The cached <see cref="PropertyChangedEventArgs"/> for <see cref="IsRunning"/>.
3434
/// </summary>
35-
internal static readonly PropertyChangedEventArgs IsRunningChangedEventArgs = new PropertyChangedEventArgs(nameof(IsRunning));
35+
internal static readonly PropertyChangedEventArgs IsRunningChangedEventArgs = new(nameof(IsRunning));
3636

3737
/// <summary>
3838
/// The <see cref="Func{TResult}"/> to invoke when <see cref="Execute"/> is used.
@@ -163,7 +163,7 @@ public Task ExecuteAsync(object? parameter)
163163
// Cancel the previous operation, if one is pending
164164
this.cancellationTokenSource?.Cancel();
165165

166-
var cancellationTokenSource = this.cancellationTokenSource = new CancellationTokenSource();
166+
CancellationTokenSource cancellationTokenSource = this.cancellationTokenSource = new();
167167

168168
OnPropertyChanged(IsCancellationRequestedChangedEventArgs);
169169

Microsoft.Toolkit.Mvvm/Input/AsyncRelayCommand{T}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public Task ExecuteAsync(T parameter)
168168
// Cancel the previous operation, if one is pending
169169
this.cancellationTokenSource?.Cancel();
170170

171-
var cancellationTokenSource = this.cancellationTokenSource = new CancellationTokenSource();
171+
CancellationTokenSource cancellationTokenSource = this.cancellationTokenSource = new();
172172

173173
OnPropertyChanged(AsyncRelayCommand.IsCancellationRequestedChangedEventArgs);
174174

Microsoft.Toolkit.Mvvm/Messaging/IMessengerExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ private static class DiscoveredRecipients<TToken>
4747
/// <summary>
4848
/// The <see cref="ConditionalWeakTable{TKey,TValue}"/> instance used to track the preloaded registration actions for each recipient.
4949
/// </summary>
50-
public static readonly ConditionalWeakTable<Type, Action<IMessenger, object, TToken>[]> RegistrationMethods
51-
= new ConditionalWeakTable<Type, Action<IMessenger, object, TToken>[]>();
50+
public static readonly ConditionalWeakTable<Type, Action<IMessenger, object, TToken>[]> RegistrationMethods = new();
5251
}
5352

5453
/// <summary>

Microsoft.Toolkit.Mvvm/Messaging/Internals/Microsoft.Collections.Extensions/DictionarySlim{TKey,TValue}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ private Entry[] Resize()
334334
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
335335
[Pure]
336336
[MethodImpl(MethodImplOptions.AggressiveInlining)]
337-
public Enumerator GetEnumerator() => new Enumerator(this);
337+
public Enumerator GetEnumerator() => new(this);
338338

339339
/// <summary>
340340
/// Enumerator for <see cref="DictionarySlim{TKey,TValue}"/>.

Microsoft.Toolkit.Mvvm/Messaging/Messages/AsyncCollectionRequestMessage{T}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public class AsyncCollectionRequestMessage<T> : IAsyncEnumerable<T>
2222
/// operations that can be executed in parallel, or <see cref="Func{T,TResult}"/> instances, which can be used so that multiple
2323
/// asynchronous operations are only started sequentially from <see cref="GetAsyncEnumerator"/> and do not overlap in time.
2424
/// </summary>
25-
private readonly List<(Task<T>?, Func<CancellationToken, Task<T>>?)> responses = new List<(Task<T>?, Func<CancellationToken, Task<T>>?)>();
25+
private readonly List<(Task<T>?, Func<CancellationToken, Task<T>>?)> responses = new();
2626

2727
/// <summary>
2828
/// The <see cref="CancellationTokenSource"/> instance used to link the token passed to
2929
/// <see cref="GetAsyncEnumerator"/> and the one passed to all subscribers to the message.
3030
/// </summary>
31-
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
31+
private readonly CancellationTokenSource cancellationTokenSource = new();
3232

3333
/// <summary>
3434
/// Gets the <see cref="System.Threading.CancellationToken"/> instance that will be linked to the
@@ -102,7 +102,7 @@ public async Task<IReadOnlyCollection<T>> GetResponsesAsync(CancellationToken ca
102102
cancellationToken.Register(this.cancellationTokenSource.Cancel);
103103
}
104104

105-
List<T> results = new List<T>(this.responses.Count);
105+
List<T> results = new(this.responses.Count);
106106

107107
await foreach (var response in this.WithCancellation(cancellationToken))
108108
{

Microsoft.Toolkit.Mvvm/Messaging/Messages/CollectionRequestMessage{T}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.Toolkit.Mvvm.Messaging.Messages
1616
/// <typeparam name="T">The type of request to make.</typeparam>
1717
public class CollectionRequestMessage<T> : IEnumerable<T>
1818
{
19-
private readonly List<T> responses = new List<T>();
19+
private readonly List<T> responses = new();
2020

2121
/// <summary>
2222
/// Gets the message responses.

0 commit comments

Comments
 (0)