Fix IDE0025 (use expression body for properties)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / ArgumentException.cs
blobb6d4ede60745270c9d3605f8fd699bd3b7b08c6e
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.
5 /*=============================================================================
6 **
7 **
8 **
9 ** Purpose: Exception class for invalid arguments to a method.
12 =============================================================================*/
14 using System.Globalization;
15 using System.Runtime.Serialization;
17 namespace System
19 // The ArgumentException is thrown when an argument does not meet
20 // the contract of the method. Ideally it should give a meaningful error
21 // message describing what was wrong and which parameter is incorrect.
22 [Serializable]
23 [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
24 public class ArgumentException : SystemException
26 private readonly string? _paramName;
28 // Creates a new ArgumentException with its message
29 // string set to the empty string.
30 public ArgumentException()
31 : base(SR.Arg_ArgumentException)
33 HResult = HResults.COR_E_ARGUMENT;
36 // Creates a new ArgumentException with its message
37 // string set to message.
39 public ArgumentException(string? message)
40 : base(message)
42 HResult = HResults.COR_E_ARGUMENT;
45 public ArgumentException(string? message, Exception? innerException)
46 : base(message, innerException)
48 HResult = HResults.COR_E_ARGUMENT;
51 public ArgumentException(string? message, string? paramName, Exception? innerException)
52 : base(message, innerException)
54 _paramName = paramName;
55 HResult = HResults.COR_E_ARGUMENT;
58 public ArgumentException(string? message, string? paramName)
59 : base(message)
61 _paramName = paramName;
62 HResult = HResults.COR_E_ARGUMENT;
65 protected ArgumentException(SerializationInfo info, StreamingContext context)
66 : base(info, context)
68 _paramName = info.GetString("ParamName");
71 public override void GetObjectData(SerializationInfo info, StreamingContext context)
73 base.GetObjectData(info, context);
74 info.AddValue("ParamName", _paramName, typeof(string));
77 public override string Message
79 get
81 SetMessageField();
83 string s = base.Message;
84 if (!string.IsNullOrEmpty(_paramName))
86 s += " " + SR.Format(SR.Arg_ParamName_Name, _paramName);
89 return s;
93 private void SetMessageField()
95 if (_message == null && HResult == System.HResults.COR_E_ARGUMENT)
97 _message = SR.Arg_ArgumentException;
101 public virtual string? ParamName => _paramName;