Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for ToAnotherOrDefault #65

Merged
merged 15 commits into from
Jun 28, 2024
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions DocsReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,56 @@ IEnumerable<MyEnum> output = inputEnums.ToEnums<MyEnum>(); // or false if case i
// 2 - MyEnum.Second
// 3 - MyEnum.Third
```

# ToAnotherOrDefault
kurnakovv marked this conversation as resolved.
Show resolved Hide resolved

**Description**

Convert input enum to another enum or default value if not possible.

**Signature**

TAnotherEnum ToAnotherOrDefault<TAnotherEnum>(this Enum, bool [default = true], TAnotherEnum = default(TAnotherEnum)) where TAnotherEnum : struct

**Type Parameters**

`TAnotherEnum` - Enum that we want to get after convert.
kurnakovv marked this conversation as resolved.
Show resolved Hide resolved

**Parameters**

- `Enum enumValue` - Input enum we want to convert to `TAnotherEnum` if possible.
- `bool ignoreCase` - Ignore or regard case.
- `TAnotherEnum defaultValue` - default value that will convert to `TAnotherEnum` otherwise.
kurnakovv marked this conversation as resolved.
Show resolved Hide resolved

**Returns**

`TAnotherEnum` or `defaultValue` if not possible to convert.

**Code examples**
```csharp
// Enums.
public enum AnotherEnum { First, Second, Third }

public enum InputEnum { First, Second, Third }
public enum InvalidEnum { FirstInvalidValue, SecondInvalidValue }


// Input enums
InputEnum inputEnumFirst = InputEnum.First;
InvalidEnum invalidSecondValue = InvalidEnum.SecondInvalidValue;
AnotherEnum defaultValue = AnotherEnum.Third;
InputEnum inputEnumThird = InputEnum.Third;
```

```csharp
AnotherEnum anotherEnumFirstCorrect = inputEnumFirst.ToAnotherOrDefault<AnotherEnum>();
AnotherEnum anotherEnumSecondInvalid = invalidSecondValue.ToAnotherOrDefault<AnotherEnum>();
AnotherEnum anotherEnumWithDefaultValue = invalidSecondValue.ToAnotherOrDefault<AnotherEnum>(defaultValue);
AnotherEnum anotherEnumThirdWithIgnoreCase = inputEnumThird.ToAnotherOrDefault<AnotherEnum>(false);

// Output:
kurnakovv marked this conversation as resolved.
Show resolved Hide resolved
// anotherEnumFirstCorrect - First
// anotherEnumSecondInvalid - default(AnotherEnum)
// anotherEnumWithDefaultValue - defaultValue
kurnakovv marked this conversation as resolved.
Show resolved Hide resolved
// anotherEnumThirdWithIgnoreCase - Third (defaultValue)
kurnakovv marked this conversation as resolved.
Show resolved Hide resolved
```
Loading