@@ -21,7 +21,7 @@ namespace SharpConfig
2121 /// </summary>
2222 public class Configuration : IEnumerable < Section >
2323 {
24- private static CultureInfo s_cultureInfo ;
24+ private static CultureInfo ? s_cultureInfo ;
2525 private static char s_preferredCommentChar ;
2626 private static char s_arrayElementSeparator ;
2727 private static readonly Dictionary < Type , ITypeStringConverter > s_typeStringConverters ;
@@ -120,8 +120,13 @@ public Section Add(string sectionName)
120120 /// empty.</exception>
121121 public bool Remove ( string sectionName )
122122 {
123- return string . IsNullOrEmpty ( sectionName ) ? throw new ArgumentNullException ( nameof ( sectionName ) )
124- : Remove ( FindSection ( sectionName ) ) ;
123+ if ( string . IsNullOrEmpty ( sectionName ) )
124+ {
125+ throw new ArgumentNullException ( nameof ( sectionName ) ) ;
126+ }
127+
128+ var section = FindSection ( sectionName ) ;
129+ return section != null && Remove ( section ) ;
125130 }
126131
127132 /// <summary>
@@ -228,6 +233,11 @@ public static void RegisterTypeStringConverter(ITypeStringConverter converter)
228233
229234 var type = converter . ConvertibleType ;
230235
236+ if ( type == null )
237+ {
238+ throw new ArgumentException ( "The converter's ConvertibleType cannot be null." , nameof ( converter ) ) ;
239+ }
240+
231241 if ( s_typeStringConverters . ContainsKey ( type ) )
232242 {
233243 throw new InvalidOperationException ( $ "A converter for type '{ type . FullName } ' is already registered.") ;
@@ -277,7 +287,7 @@ public static ITypeStringConverter FindTypeStringConverter(Type type)
277287 type = typeof ( Enum ) ;
278288 }
279289
280- if ( ! s_typeStringConverters . TryGetValue ( type , out ITypeStringConverter converter ) )
290+ if ( ! s_typeStringConverters . TryGetValue ( type , out ITypeStringConverter ? converter ) )
281291 {
282292 converter = FallbackConverter ;
283293 }
@@ -302,7 +312,7 @@ public static ITypeStringConverter FindTypeStringConverter(Type type)
302312 /// <exception cref="ArgumentNullException">When <paramref name="filename"/> is null or empty.</exception>
303313 /// <exception cref="FileNotFoundException">When the specified configuration file is not
304314 /// found.</exception>
305- public static Configuration LoadFromFile ( string filename , Encoding encoding = null )
315+ public static Configuration LoadFromFile ( string filename , Encoding ? encoding = null )
306316 {
307317 if ( string . IsNullOrEmpty ( filename ) )
308318 {
@@ -331,7 +341,7 @@ public static Configuration LoadFromFile(string filename, Encoding encoding = nu
331341 /// </returns>
332342 ///
333343 /// <exception cref="ArgumentNullException">When <paramref name="stream"/> is null.</exception>
334- public static Configuration LoadFromStream ( Stream stream , Encoding encoding = null )
344+ public static Configuration LoadFromStream ( Stream stream , Encoding ? encoding = null )
335345 {
336346 if ( stream == null )
337347 {
@@ -374,7 +384,7 @@ public static Configuration LoadFromString(string source)
374384 /// </returns>
375385 ///
376386 /// <exception cref="ArgumentNullException">When <paramref name="filename"/> is null or empty.</exception>
377- public static Configuration LoadFromBinaryFile ( string filename , BinaryReader reader = null )
387+ public static Configuration LoadFromBinaryFile ( string filename , BinaryReader ? reader = null )
378388 {
379389 if ( string . IsNullOrEmpty ( filename ) )
380390 {
@@ -400,7 +410,7 @@ public static Configuration LoadFromBinaryFile(string filename, BinaryReader rea
400410 /// </returns>
401411 ///
402412 /// <exception cref="ArgumentNullException">When <paramref name="stream"/> is null.</exception>
403- public static Configuration LoadFromBinaryStream ( Stream stream , BinaryReader reader = null )
413+ public static Configuration LoadFromBinaryStream ( Stream stream , BinaryReader ? reader = null )
404414 {
405415 return stream == null ? throw new ArgumentNullException ( nameof ( stream ) )
406416 : ConfigurationReader . ReadFromBinaryStream ( stream , reader ) ;
@@ -411,7 +421,7 @@ public static Configuration LoadFromBinaryStream(Stream stream, BinaryReader rea
411421 /// </summary>
412422 ///
413423 /// <param name="filename">The location of the configuration file.</param>
414- public void SaveToFile ( string filename ) => SaveToFile ( filename , null ) ;
424+ public void SaveToFile ( string filename ) => SaveToFile ( filename , ( Encoding ? ) null ) ;
415425
416426 /// <summary>
417427 /// Saves the configuration to a file.
@@ -422,7 +432,7 @@ public static Configuration LoadFromBinaryStream(Stream stream, BinaryReader rea
422432 /// is UTF8.</param>
423433 ///
424434 /// <exception cref="ArgumentNullException">When <paramref name="filename"/> is null or empty.</exception>
425- public void SaveToFile ( string filename , Encoding encoding )
435+ public void SaveToFile ( string filename , Encoding ? encoding )
426436 {
427437 if ( string . IsNullOrEmpty ( filename ) )
428438 {
@@ -440,7 +450,7 @@ public void SaveToFile(string filename, Encoding encoding)
440450 /// </summary>
441451 ///
442452 /// <param name="stream">The stream to save the configuration to.</param>
443- public void SaveToStream ( Stream stream ) => SaveToStream ( stream , null ) ;
453+ public void SaveToStream ( Stream stream ) => SaveToStream ( stream , ( Encoding ? ) null ) ;
444454
445455 /// <summary>
446456 /// Saves the configuration to a stream.
@@ -451,7 +461,7 @@ public void SaveToFile(string filename, Encoding encoding)
451461 /// is UTF8.</param>
452462 ///
453463 /// <exception cref="ArgumentNullException">When <paramref name="stream"/> is null.</exception>
454- public void SaveToStream ( Stream stream , Encoding encoding )
464+ public void SaveToStream ( Stream stream , Encoding ? encoding )
455465 {
456466 if ( stream == null )
457467 {
@@ -466,7 +476,7 @@ public void SaveToStream(Stream stream, Encoding encoding)
466476 /// </summary>
467477 ///
468478 /// <param name="filename">The location of the configuration file.</param>
469- public void SaveToBinaryFile ( string filename ) => SaveToBinaryFile ( filename , null ) ;
479+ public void SaveToBinaryFile ( string filename ) => SaveToBinaryFile ( filename , ( BinaryWriter ? ) null ) ;
470480
471481 /// <summary>
472482 /// Saves the configuration to a binary file, using a specific <see cref="BinaryWriter"/>.
@@ -476,7 +486,7 @@ public void SaveToStream(Stream stream, Encoding encoding)
476486 /// <param name="writer"> The writer to use. Specify null to use the default writer.</param>
477487 ///
478488 /// <exception cref="ArgumentNullException">When <paramref name="filename"/> is null or empty.</exception>
479- public void SaveToBinaryFile ( string filename , BinaryWriter writer )
489+ public void SaveToBinaryFile ( string filename , BinaryWriter ? writer )
480490 {
481491 if ( string . IsNullOrEmpty ( filename ) )
482492 {
@@ -494,7 +504,7 @@ public void SaveToBinaryFile(string filename, BinaryWriter writer)
494504 /// </summary>
495505 ///
496506 /// <param name="stream">The stream to save the configuration to.</param>
497- public void SaveToBinaryStream ( Stream stream ) => SaveToBinaryStream ( stream , null ) ;
507+ public void SaveToBinaryStream ( Stream stream ) => SaveToBinaryStream ( stream , ( BinaryWriter ? ) null ) ;
498508
499509 /// <summary>
500510 /// Saves the configuration to a binary file, using a specific <see cref="BinaryWriter"/>.
@@ -504,7 +514,7 @@ public void SaveToBinaryFile(string filename, BinaryWriter writer)
504514 /// <param name="writer">The writer to use. Specify null to use the default writer.</param>
505515 ///
506516 /// <exception cref="ArgumentNullException">When <paramref name="stream"/> is null.</exception>
507- public void SaveToBinaryStream ( Stream stream , BinaryWriter writer )
517+ public void SaveToBinaryStream ( Stream stream , BinaryWriter ? writer )
508518 {
509519 if ( stream == null )
510520 {
@@ -580,15 +590,15 @@ void WriteSection(Section section)
580590 /// <exception cref="ArgumentNullException">When a null reference is set.</exception>
581591 public static CultureInfo CultureInfo
582592 {
583- get => s_cultureInfo ;
593+ get => s_cultureInfo ! ;
584594 set => s_cultureInfo = value ?? throw new ArgumentNullException ( nameof ( value ) ) ;
585595 }
586596
587597 /// <summary>
588598 /// Gets the array that contains all valid comment delimiting characters.
589599 /// The default value is { '#', ';' }.
590600 /// </summary>
591- public static HashSet < char > ValidCommentChars { get ; private set ; }
601+ public static HashSet < char > ValidCommentChars { get ; private set ; } = null ! ;
592602
593603 /// <summary>
594604 /// Gets or sets the preferred comment char when saving configurations.
@@ -603,7 +613,7 @@ public static char PreferredCommentChar
603613 {
604614 if ( ! ValidCommentChars . Contains ( value ) )
605615 {
606- throw new ArgumentException ( "The specified char '" + value + " ' is not allowed as a comment char.") ;
616+ throw new ArgumentException ( $ "The specified char '{ value } ' is not allowed as a comment char.") ;
607617 }
608618
609619 s_preferredCommentChar = value ;
@@ -749,7 +759,7 @@ public IEnumerable<Section> GetSectionsNamed(string name)
749759 }
750760
751761 // Finds a section by its name.
752- private Section FindSection ( string name )
762+ private Section ? FindSection ( string name )
753763 {
754764 return _sections . FirstOrDefault (
755765 section => string . Equals ( section . Name , name , StringComparison . OrdinalIgnoreCase ) ) ;
0 commit comments