Skip to content

Commit 73e5041

Browse files
authored
Enable nullability and fix warnings (#9016)
1 parent 002f9c4 commit 73e5041

File tree

79 files changed

+791
-991
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+791
-991
lines changed

snippets/csharp/Microsoft.Extensions.DependencyModel/DependencyContext/Overview/DependencyContextSnippets.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

snippets/csharp/Microsoft.SqlServer.Server/SqlFunctionAttribute/Function.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Library</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

snippets/csharp/Microsoft.SqlServer.Server/SqlUserDefinedAggregateAttribute/Aggregate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Library</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

snippets/csharp/Microsoft.SqlServer.Server/SqlUserDefinedTypeAttribute/Type.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Library</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

snippets/csharp/Microsoft.SqlServer.Server/SqlUserDefinedTypeAttribute/Type1.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ public Int32 Y
3636
}
3737
}
3838

39-
public bool IsNull
40-
{
41-
get
42-
{
43-
return is_Null;
44-
}
45-
}
39+
public bool IsNull => is_Null;
4640

4741
public static Point Null
4842
{
@@ -74,12 +68,15 @@ public static Point Parse(SqlString s)
7468
}
7569

7670
// Parse input string here to separate out coordinates
77-
string str = Convert.ToString(s);
78-
string[] xy = str.Split(':');
71+
string? str = Convert.ToString(s);
72+
string[]? xy = str?.Split(':');
7973

8074
Point pt = new Point();
81-
pt.X = Convert.ToInt32(xy[0]);
82-
pt.Y = Convert.ToInt32(xy[1]);
75+
if (xy is not null)
76+
{
77+
pt.X = Convert.ToInt32(xy[0]);
78+
pt.Y = Convert.ToInt32(xy[1]);
79+
}
8380
return (pt);
8481
}
8582

@@ -125,9 +122,9 @@ public SqlString Quadrant()
125122

126123
//-----------------------------------------------------------------------------
127124
//<Snippet12>
128-
[SqlUserDefinedType(Format.Native, MaxByteSize=8000)]
125+
[SqlUserDefinedType(Format.Native, MaxByteSize = 8000)]
129126
public class SampleType
130127
{
131-
//...
128+
//...
132129
}
133130
//</Snippet12>

snippets/csharp/System.Collections.Immutable/ImmutableArray`1/Overview/ImmutableArraySnippets.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net7.0</TargetFramework>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
</Project>

snippets/csharp/System.DateTime/DateWithTimeZone.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

snippets/csharp/System.DateTime/Persistence.cs

Lines changed: 15 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using DateTimeExtensions;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Globalization;
54
using System.IO;
6-
using System.Runtime.Serialization;
7-
using System.Runtime.Serialization.Formatters.Binary;
85
using System.Threading;
96
using System.Xml.Serialization;
107

@@ -29,13 +26,6 @@ public static void Snippets()
2926
File.Delete(filenameXml);
3027
PersistAsXML();
3128
File.Delete(filenameXml);
32-
33-
File.Delete(filenameBin);
34-
PersistBinary();
35-
File.Delete(filenameBin);
36-
37-
SaveDateWithTimeZone();
38-
RestoreDateWithTimeZone();
3929
}
4030

4131
// <Snippet1>
@@ -52,7 +42,7 @@ private static void SaveLocalDatesAsString()
5242
new DateTime(2015, 1, 10, 1, 16, 0),
5343
new DateTime(2014, 12, 20, 21, 45, 0),
5444
new DateTime(2014, 6, 2, 15, 14, 0) };
55-
string output = null;
45+
string? output = null;
5646

5747
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
5848
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
@@ -87,7 +77,7 @@ private static void RestoreLocalDatesFromString()
8777
}
8878
else
8979
{
90-
Console.WriteLine("Cannot parse '{inputValue}'");
80+
Console.WriteLine($"Cannot parse '{inputValue}'");
9181
}
9282
}
9383
Console.WriteLine("Restored dates...");
@@ -127,7 +117,7 @@ private static void SaveDatesAsInvariantStrings()
127117
new DateTime(2015, 1, 10, 1, 16, 0),
128118
new DateTime(2014, 12, 20, 21, 45, 0),
129119
new DateTime(2014, 6, 2, 15, 14, 0) };
130-
string output = null;
120+
string? output = null;
131121

132122
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
133123
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
@@ -318,29 +308,32 @@ public static void PersistAsXML()
318308
}
319309
catch (InvalidOperationException e)
320310
{
321-
Console.WriteLine(e.InnerException.Message);
311+
Console.WriteLine(e.InnerException?.Message);
322312
}
323313
finally
324314
{
325315
if (sw != null) sw.Close();
326316
}
327317

328318
// Deserialize the data.
329-
DateTime[] deserializedDates;
319+
DateTime[]? deserializedDates;
330320
using (var fs = new FileStream(filenameXml, FileMode.Open))
331321
{
332-
deserializedDates = (DateTime[])serializer.Deserialize(fs);
322+
deserializedDates = (DateTime[]?)serializer.Deserialize(fs);
333323
}
334324

335325
// Display the dates.
336326
Console.WriteLine($"Leap year days from 2000-2100 on an {Thread.CurrentThread.CurrentCulture.Name} system:");
337327
int nItems = 0;
338-
foreach (var dat in deserializedDates)
328+
if (deserializedDates is not null)
339329
{
340-
Console.Write($" {dat:d} ");
341-
nItems++;
342-
if (nItems % 5 == 0)
343-
Console.WriteLine();
330+
foreach (var dat in deserializedDates)
331+
{
332+
Console.Write($" {dat:d} ");
333+
nItems++;
334+
if (nItems % 5 == 0)
335+
Console.WriteLine();
336+
}
344337
}
345338
}
346339
// The example displays the following output:
@@ -351,156 +344,5 @@ public static void PersistAsXML()
351344
// 29/02/2060 29/02/2064 29/02/2068 29/02/2072 29/02/2076
352345
// 29/02/2080 29/02/2084 29/02/2088 29/02/2092 29/02/2096
353346
// </Snippet4>
354-
355-
private const string filenameBin = @".\Dates.bin";
356-
357-
// <Snippet5>
358-
public static void PersistBinary()
359-
{
360-
SaveDatesBinary();
361-
RestoreDatesBinary();
362-
}
363-
364-
private static void SaveDatesBinary()
365-
{
366-
DateTime[] dates = { new DateTime(2014, 6, 14, 6, 32, 0),
367-
new DateTime(2014, 7, 10, 23, 49, 0),
368-
new DateTime(2015, 1, 10, 1, 16, 0),
369-
new DateTime(2014, 12, 20, 21, 45, 0),
370-
new DateTime(2014, 6, 2, 15, 14, 0) };
371-
var fs = new FileStream(filenameBin, FileMode.Create);
372-
var bin = new BinaryFormatter();
373-
374-
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
375-
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
376-
for (int ctr = 0; ctr < dates.Length; ctr++)
377-
{
378-
Console.WriteLine(dates[ctr].ToString("f"));
379-
dates[ctr] = dates[ctr].ToUniversalTime();
380-
}
381-
bin.Serialize(fs, dates);
382-
fs.Close();
383-
Console.WriteLine("Saved dates...");
384-
}
385-
386-
private static void RestoreDatesBinary()
387-
{
388-
TimeZoneInfo.ClearCachedData();
389-
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
390-
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
391-
392-
FileStream fs = new FileStream(filenameBin, FileMode.Open);
393-
BinaryFormatter bin = new BinaryFormatter();
394-
var dates = (DateTime[])bin.Deserialize(fs);
395-
fs.Close();
396-
397-
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
398-
foreach (var value in dates)
399-
Console.WriteLine(value.ToLocalTime().ToString("f"));
400-
401-
Console.WriteLine("Restored dates...");
402-
}
403-
// When saved on an en-US system, the example displays the following output:
404-
// Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
405-
// The dates on an en-US system:
406-
// Saturday, June 14, 2014 6:32 AM
407-
// Thursday, July 10, 2014 11:49 PM
408-
// Saturday, January 10, 2015 1:16 AM
409-
// Saturday, December 20, 2014 9:45 PM
410-
// Monday, June 02, 2014 3:14 PM
411-
// Saved dates...
412-
//
413-
// When restored on an en-GB system, the example displays the following output:
414-
// Current Time Zone: (UTC-6:00) Central Time (US & Canada)
415-
// The dates on an en-GB system:
416-
// 14 June 2014 08:32
417-
// 11 July 2014 01:49
418-
// 10 January 2015 03:16
419-
// 20 December 2014 23:45
420-
// 02 June 2014 17:14
421-
// Restored dates...
422-
// </Snippet5>
423-
424-
// <Snippet7>
425-
public static void SaveDateWithTimeZone()
426-
{
427-
DateWithTimeZone[] dates = { new DateWithTimeZone(new DateTime(2014, 8, 9, 19, 30, 0),
428-
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
429-
new DateWithTimeZone(new DateTime(2014, 8, 15, 19, 0, 0),
430-
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")),
431-
new DateWithTimeZone(new DateTime(2014, 8, 22, 19, 30, 0),
432-
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
433-
new DateWithTimeZone(new DateTime(2014, 8, 28, 19, 0, 0),
434-
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")) };
435-
var fs = new FileStream(@".\Schedule.bin", FileMode.Create);
436-
var formatter = new BinaryFormatter();
437-
try
438-
{
439-
formatter.Serialize(fs, dates);
440-
// Display dates.
441-
foreach (var date in dates)
442-
{
443-
TimeZoneInfo tz = date.TimeZone;
444-
Console.WriteLine($"{date.DateTime} {(tz.IsDaylightSavingTime(date.DateTime) ? tz.DaylightName : tz.StandardName)}");
445-
}
446-
}
447-
catch (SerializationException e)
448-
{
449-
Console.WriteLine($"Serialization failed. Reason: {e.Message}");
450-
}
451-
finally
452-
{
453-
if (fs != null) fs.Close();
454-
}
455-
}
456-
// The example displays the following output:
457-
// 8/9/2014 7:30:00 PM Eastern Daylight Time
458-
// 8/15/2014 7:00:00 PM Pacific Daylight Time
459-
// 8/22/2014 7:30:00 PM Eastern Daylight Time
460-
// 8/28/2014 7:00:00 PM Eastern Daylight Time
461-
// </Snippet7>
462-
463-
// <Snippet8>
464-
public static void RestoreDateWithTimeZone()
465-
{
466-
const string filename = @".\Schedule.bin";
467-
FileStream fs;
468-
if (File.Exists(filename))
469-
{
470-
fs = new FileStream(filename, FileMode.Open);
471-
}
472-
else
473-
{
474-
Console.WriteLine("Unable to find file to deserialize.");
475-
return;
476-
}
477-
478-
var formatter = new BinaryFormatter();
479-
DateWithTimeZone[] dates;
480-
try
481-
{
482-
dates = (DateWithTimeZone[])formatter.Deserialize(fs);
483-
// Display dates.
484-
foreach (var date in dates)
485-
{
486-
TimeZoneInfo tz = date.TimeZone;
487-
Console.WriteLine($"{ date.DateTime} {(tz.IsDaylightSavingTime(date.DateTime) ? tz.DaylightName : tz.StandardName)}");
488-
}
489-
}
490-
catch (SerializationException e)
491-
{
492-
Console.WriteLine($"Deserialization failed. Reason: {e.Message}");
493-
}
494-
finally
495-
{
496-
if (fs != null) fs.Close();
497-
}
498-
}
499-
// The example displays the following output:
500-
// 8/9/2014 7:30:00 PM Eastern Daylight Time
501-
// 8/15/2014 7:00:00 PM Pacific Daylight Time
502-
// 8/22/2014 7:30:00 PM Eastern Daylight Time
503-
// 8/28/2014 7:00:00 PM Eastern Daylight Time
504-
// </Snippet8>
505347
}
506348
}

snippets/csharp/System.DateTime/SystemDateTimeReference.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
</Project>

snippets/csharp/System.Diagnostics.Tracing/EventSource/Overview/etwtracesmall.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6+
<Nullable>enable</Nullable>
67
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
78
<StartupObject>Demo.Program</StartupObject>
89
</PropertyGroup>

0 commit comments

Comments
 (0)