• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using System;
11 
12 namespace Google.Protobuf
13 {
14     /// <summary>
15     /// Helper methods for throwing exceptions when preconditions are not met.
16     /// </summary>
17     /// <remarks>
18     /// This class is used internally and by generated code; it is not particularly
19     /// expected to be used from application code, although nothing prevents it
20     /// from being used that way.
21     /// </remarks>
22     public static class ProtoPreconditions
23     {
24         /// <summary>
25         /// Throws an ArgumentNullException if the given value is null, otherwise
26         /// return the value to the caller.
27         /// </summary>
28         public static T CheckNotNull<T>(T value, string name) where T : class
29         {
30             if (value == null)
31             {
32                 throw new ArgumentNullException(name);
33             }
34             return value;
35         }
36 
37         /// <summary>
38         /// Throws an ArgumentNullException if the given value is null, otherwise
39         /// return the value to the caller.
40         /// </summary>
41         /// <remarks>
42         /// This is equivalent to <see cref="CheckNotNull{T}(T, string)"/> but without the type parameter
43         /// constraint. In most cases, the constraint is useful to prevent you from calling CheckNotNull
44         /// with a value type - but it gets in the way if either you want to use it with a nullable
45         /// value type, or you want to use it with an unconstrained type parameter.
46         /// </remarks>
CheckNotNullUnconstrained(T value, string name)47         internal static T CheckNotNullUnconstrained<T>(T value, string name)
48         {
49             if (value == null)
50             {
51                 throw new ArgumentNullException(name);
52             }
53             return value;
54         }
55     }
56 }