Skip to content

Commit 51c6bab

Browse files
committed
Enabled nullability annotations in .Core project
1 parent 1f6fbc5 commit 51c6bab

File tree

9 files changed

+51
-30
lines changed

9 files changed

+51
-30
lines changed

samples/MvvmSample.Core/Models/Post.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,55 @@ namespace MvvmSample.Core.Models
1111
/// <summary>
1212
/// A class for a query for posts in a given subreddit.
1313
/// </summary>
14-
public class PostsQueryResponse
14+
public sealed class PostsQueryResponse
1515
{
1616
/// <summary>
1717
/// Gets or sets the listing data for the response.
1818
/// </summary>
1919
[JsonPropertyName("data")]
20-
public PostListing Data { get; set; }
20+
public PostListing? Data { get; init; }
2121
}
2222

2323
/// <summary>
2424
/// A class for a Reddit listing of posts.
2525
/// </summary>
26-
public class PostListing
26+
public sealed class PostListing
2727
{
2828
/// <summary>
2929
/// Gets or sets the items in this listing.
3030
/// </summary>
3131
[JsonPropertyName("children")]
32-
public IList<PostData> Items { get; set; }
32+
public IList<PostData>? Items { get; init; }
3333
}
3434

3535
/// <summary>
3636
/// A wrapping class for a post.
3737
/// </summary>
38-
public class PostData
38+
public sealed class PostData
3939
{
4040
/// <summary>
4141
/// Gets or sets the <see cref="Post"/> instance.
4242
/// </summary>
4343
[JsonPropertyName("data")]
44-
public Post Data { get; set; }
44+
public Post? Data { get; init; }
4545
}
4646

4747
/// <summary>
4848
/// A simple model for a Reddit post.
4949
/// </summary>
50-
public class Post
50+
public sealed class Post
5151
{
5252
/// <summary>
5353
/// Gets or sets the title of the post.
5454
/// </summary>
5555
[JsonPropertyName("title")]
56-
public string Title { get; set; }
56+
public string? Title { get; init; }
5757

5858
/// <summary>
5959
/// Gets or sets the URL to the post thumbnail, if present.
6060
/// </summary>
6161
[JsonPropertyName("thumbnail")]
62-
public string Thumbnail { get; set; }
62+
public string? Thumbnail { get; init; }
6363

6464
/// <summary>
6565
/// Gets the text of the post.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.ComponentModel;
6+
7+
namespace System.Runtime.CompilerServices
8+
{
9+
/// <summary>
10+
/// Reserved to be used by the compiler for tracking metadata.
11+
/// This class should not be used by developers in source code.
12+
/// </summary>
13+
[EditorBrowsable(EditorBrowsableState.Never)]
14+
internal static class IsExternalInit
15+
{
16+
}
17+
}

samples/MvvmSample.Core/MvvmSample.Core.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<LangVersion>8.0</LangVersion>
5+
<LangVersion>9.0</LangVersion>
6+
<Nullable>enable</Nullable>
67
<UserSecretsId>4e66f7b4-01a8-4f00-8733-4ae6a08c741f</UserSecretsId>
78
</PropertyGroup>
89

samples/MvvmSample.Core/Services/ISettingsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public interface ISettingsService
2626
/// <typeparam name="T">The type of the object to retrieve.</typeparam>
2727
/// <param name="key">The key associated to the requested object.</param>
2828
[Pure]
29-
T GetValue<T>(string key);
29+
T? GetValue<T>(string key);
3030
}
3131
}

samples/MvvmSample.Core/ViewModels/MessengerPageViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ protected override void OnActivated()
7373
}
7474
}
7575

76-
private string username;
76+
private string? username;
7777

78-
public string Username
78+
public string? Username
7979
{
8080
get => username;
8181
private set => SetProperty(ref username, value);

samples/MvvmSample.Core/ViewModels/ObservableObjectPageViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ public ObservableObjectPageViewModel()
2020
/// </summary>
2121
public ICommand ReloadTaskCommand { get; }
2222

23-
private string name;
23+
private string? name;
2424

2525
/// <summary>
2626
/// Gets or sets the name to display.
2727
/// </summary>
28-
public string Name
28+
public string? Name
2929
{
3030
get => name;
3131
set => SetProperty(ref name, value);
3232
}
3333

34-
private TaskNotifier myTask;
34+
private TaskNotifier? myTask;
3535

3636
/// <summary>
3737
/// Gets or sets the name to display.
3838
/// </summary>
39-
public Task MyTask
39+
public Task? MyTask
4040
{
4141
get => myTask;
4242
private set => SetPropertyAndNotifyOnCompletion(ref myTask, value);

samples/MvvmSample.Core/ViewModels/SamplePageViewModel.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace MvvmSample.Core.ViewModels
1818
/// </summary>
1919
public class SamplePageViewModel : ObservableObject
2020
{
21-
private IReadOnlyDictionary<string, string> texts;
2221
/// <summary>
2322
/// The <see cref="IFilesService"/> instance currently in use.
2423
/// </summary>
@@ -34,7 +33,9 @@ public SamplePageViewModel()
3433
/// </summary>
3534
public IAsyncRelayCommand<string> LoadDocsCommand { get; }
3635

37-
public IReadOnlyDictionary<string, string> Texts
36+
private IReadOnlyDictionary<string, string>? texts;
37+
38+
public IReadOnlyDictionary<string, string>? Texts
3839
{
3940
get => texts;
4041
set => SetProperty(ref texts, value);
@@ -47,15 +48,17 @@ public IReadOnlyDictionary<string, string> Texts
4748
/// <returns>The text of the specified paragraph, or <see langword="null"/>.</returns>
4849
public string GetParagraph(string key)
4950
{
50-
return Texts != null && Texts.TryGetValue(key, out var value) ? value : string.Empty;
51+
return Texts is not null && Texts.TryGetValue(key, out var value) ? value : string.Empty;
5152
}
5253

5354
/// <summary>
5455
/// Implements the logic for <see cref="LoadDocsCommand"/>.
5556
/// </summary>
5657
/// <param name="name">The name of the docs file to load.</param>
57-
private async Task LoadDocsAsync(string name)
58+
private async Task LoadDocsAsync(string? name)
5859
{
60+
if (name is null) return;
61+
5962
// Skip if the loading has already started
6063
if (!(LoadDocsCommand.ExecutionTask is null)) return;
6164

samples/MvvmSample.Core/ViewModels/Widgets/PostWidgetViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ namespace MvvmSample.Core.ViewModels.Widgets
1414
/// </summary>
1515
public sealed class PostWidgetViewModel : ObservableRecipient, IRecipient<PropertyChangedMessage<Post>>
1616
{
17-
private Post post;
17+
private Post? post;
1818

1919
/// <summary>
2020
/// Gets the currently selected post, if any.
2121
/// </summary>
22-
public Post Post
22+
public Post? Post
2323
{
2424
get => post;
2525
private set => SetProperty(ref post, value);

samples/MvvmSample.Core/ViewModels/Widgets/SubredditWidgetViewModel.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public sealed class SubredditWidgetViewModel : ObservableRecipient
3232
/// <summary>
3333
/// An <see cref="AsyncLock"/> instance to avoid concurrent requests.
3434
/// </summary>
35-
private readonly AsyncLock LoadingLock = new AsyncLock();
35+
private readonly AsyncLock LoadingLock = new();
3636

3737
/// <summary>
3838
/// Creates a new <see cref="SubredditWidgetViewModel"/> instance.
@@ -52,7 +52,7 @@ public SubredditWidgetViewModel()
5252
/// <summary>
5353
/// Gets the collection of loaded posts.
5454
/// </summary>
55-
public ObservableCollection<Post> Posts { get; } = new ObservableCollection<Post>();
55+
public ObservableCollection<Post> Posts { get; } = new();
5656

5757
/// <summary>
5858
/// Gets the collection of available subreddits to pick from.
@@ -83,12 +83,12 @@ public string SelectedSubreddit
8383
}
8484
}
8585

86-
private Post selectedPost;
86+
private Post? selectedPost;
8787

8888
/// <summary>
89-
/// Gets or sets the currently selected subreddit.
89+
/// Gets or sets the currently selected post, if any.
9090
/// </summary>
91-
public Post SelectedPost
91+
public Post? SelectedPost
9292
{
9393
get => selectedPost;
9494
set => SetProperty(ref selectedPost, value, true);
@@ -107,9 +107,9 @@ private async Task LoadPostsAsync()
107107

108108
Posts.Clear();
109109

110-
foreach (var item in response.Data.Items)
110+
foreach (var item in response.Data!.Items!)
111111
{
112-
Posts.Add(item.Data);
112+
Posts.Add(item.Data!);
113113
}
114114
}
115115
catch

0 commit comments

Comments
 (0)