# Class Utilities (1.73.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [1.73.0 (latest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Util.Utilities)
- [1.69.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.69.0/Google.Apis.Util.Utilities)
- [1.68.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.68.0/Google.Apis.Util.Utilities)
- [1.60.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.60.0/Google.Apis.Util.Utilities)
- [1.59.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.59.0/Google.Apis.Util.Utilities)
- [1.55.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.55.0/Google.Apis.Util.Utilities)
- [1.50.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.50.0/Google.Apis.Util.Utilities)

    public static class Utilities

A utility class which contains helper methods and extension methods.

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> Utilities

## Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Apis.Util](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Util)

## Assembly

Google.Apis.Core.dll

## Methods

### CheckArgument\<T1, T2\>(bool, string, string, T1, T2)

    public static void CheckArgument<T1, T2>(bool condition, string paramName, string format, T1 arg0, T2 arg1)

Checks that given argument-based condition is met, throwing an [ArgumentException](https://learn.microsoft.com/dotnet/api/system.argumentexception) otherwise.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `condition` | `https://learn.microsoft.com/dotnet/api/system.boolean` The (already evaluated) condition to check. |
| `paramName` | `https://learn.microsoft.com/dotnet/api/system.string` The name of the parameter whose value is being tested. |
| `format` | `https://learn.microsoft.com/dotnet/api/system.string` The format string to use to create the exception message if the condition is not met. |
| `arg0` | `T1` The first argument to the format string. |
| `arg1` | `T2` The second argument to the format string. |

| **Type Parameters** ||
|---|---|
| **Name** | **Description** |
| `T1` |   |
| `T2` |   |

### CheckEnumValue\<T\>(T, string)

    public static T CheckEnumValue<T>(T value, string paramName) where T : struct

Checks that the given value is in fact defined in the enum used as the type argument of the method.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `value` | `T` The value to check. |
| `paramName` | `https://learn.microsoft.com/dotnet/api/system.string` The name of the parameter whose value is being tested. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | `value` if it was a defined value |

| **Type Parameter** ||
|---|---|
| **Name** | **Description** |
| `T` | The enum type to check the value within. |

### ConvertToString(object)

    public static string ConvertToString(object o)

Tries to convert the specified object to a string. Uses custom type converters if available.
Returns null for a null object.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `o` | `https://learn.microsoft.com/dotnet/api/system.object` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### DeserializeForGoogleFormat(string)

    public static object DeserializeForGoogleFormat(string rawValue)

Deserializes the given raw value to an object using [Instance](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Json.NewtonsoftJsonSerializer#Google_Apis_Json_NewtonsoftJsonSerializer_Instance),
as if it were a JSON string value.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `rawValue` | `https://learn.microsoft.com/dotnet/api/system.string` The string value to deserialize. May be null, in which case null is returned. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.object` | The deserialized value. |

### GetCustomAttribute\<T\>(MemberInfo)

    public static T GetCustomAttribute<T>(this MemberInfo info) where T : Attribute

A Google.Apis utility method for returning the first matching custom attribute (or `null`) of the specified member.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `info` | `https://learn.microsoft.com/dotnet/api/system.reflection.memberinfo` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` |   |

| **Type Parameter** ||
|---|---|
| **Name** | **Description** |
| `T` |   |

### GetDateTimeFromString(string)

    public static DateTime? GetDateTimeFromString(string raw)

Parses the input string and returns [DateTime](https://learn.microsoft.com/dotnet/api/system.datetime) if the input is a valid
representation of a date. Otherwise it returns `null`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `raw` | `https://learn.microsoft.com/dotnet/api/system.string` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.datetime` |   |

### GetDateTimeOffsetFromString(string)

    public static DateTimeOffset? GetDateTimeOffsetFromString(string raw)

Parses the input string and returns [DateTimeOffset](https://learn.microsoft.com/dotnet/api/system.datetimeoffset) if the input is
of the format "yyyy-MM-ddTHH:mm:ss.FFFZ" or "yyyy-MM-ddTHH:mm:ssZ". If the input is null,
this method returns `null`. Otherwise, [FormatException](https://learn.microsoft.com/dotnet/api/system.formatexception) is thrown.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `raw` | `https://learn.microsoft.com/dotnet/api/system.string` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.datetimeoffset` |   |

### GetEnumStringValue(Enum)

    public static string GetEnumStringValue(Enum value)

Returns the defined string value of an Enum. Use for test purposes or in other Google.Apis projects.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `value` | `https://learn.microsoft.com/dotnet/api/system.enum` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### GetLibraryVersion()

    public static string GetLibraryVersion()

Returns the version of the core library.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### GetStringFromDateTime(DateTime?)

    public static string GetStringFromDateTime(DateTime? date)

Returns a string (by RFC3339) form the input [DateTime](https://learn.microsoft.com/dotnet/api/system.datetime) instance.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `date` | `https://learn.microsoft.com/dotnet/api/system.datetime` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### GetStringFromDateTimeOffset(DateTimeOffset?)

    public static string GetStringFromDateTimeOffset(DateTimeOffset? date)

Returns a string from the input [DateTimeOffset](https://learn.microsoft.com/dotnet/api/system.datetimeoffset) instance, or `null` if
`date` is null. The string is always in the format "yyyy-MM-ddTHH:mm:ss.fffZ" or
"yyyy-MM-ddTHH:mm:ssZ" - always UTC, always either second or millisecond precision, and always using the
invariant culture.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `date` | `https://learn.microsoft.com/dotnet/api/system.datetimeoffset` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### SerializeForGoogleFormat(object)

    public static string SerializeForGoogleFormat(object value)

Serializes the given value using [Instance](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Json.NewtonsoftJsonSerializer#Google_Apis_Json_NewtonsoftJsonSerializer_Instance).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `value` | `https://learn.microsoft.com/dotnet/api/system.object` The value to serialize. May be null, in which case null is returned. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` | The string representation of the object. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.argumentexception` | The value does not serialize to a JSON string. |

### ThrowIfNullOrEmpty(string, string)

    public static string ThrowIfNullOrEmpty(this string str, string paramName)

A Google.Apis utility method for throwing an [ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception) if the string is
`null` or empty.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `str` | `https://learn.microsoft.com/dotnet/api/system.string` |
| `paramName` | `https://learn.microsoft.com/dotnet/api/system.string` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` | The original string. |

### ThrowIfNull\<T\>(T, string)

    public static T ThrowIfNull<T>(this T obj, string paramName)

A Google.Apis utility method for throwing an [ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception) if the object is
`null`.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `obj` | `T` |
| `paramName` | `https://learn.microsoft.com/dotnet/api/system.string` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` |   |

| **Type Parameter** ||
|---|---|
| **Name** | **Description** |
| `T` |   |