https://github.com/bomret/unit
A type that allows only one value, also known as void.
https://github.com/bomret/unit
csharp dotnet nothing unit void
Last synced: 10 months ago
JSON representation
A type that allows only one value, also known as void.
- Host: GitHub
- URL: https://github.com/bomret/unit
- Owner: Bomret
- License: unlicense
- Created: 2015-03-08T11:05:42.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2018-02-28T13:13:29.000Z (about 8 years ago)
- Last Synced: 2024-12-05T22:34:26.597Z (over 1 year ago)
- Topics: csharp, dotnet, nothing, unit, void
- Language: F#
- Homepage: http://bomret.github.io/Unit/index.html
- Size: 448 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/TheVoid/)
|CI Provider|Status|
|----------|------------|
|Travis CI|[](https://travis-ci.org/Bomret/Unit)|
|AppVeyor|[](https://ci.appveyor.com/project/StefanReichel/unit)|
# Unit
The Unit type allows only one value which is empty. This is similar to the `System.Void` type (`void` keyword) which can sadly not be used by user code. Using the Unit type as return value instead of `void` offers some benefits, such as the deprecation of non-generic or special classes for the purpose of communicating the absence of a return value (`Task` would replace `Task`, `Func` et al. would make `Action` et al. obsolete).
Other languages, such as F#, Haskell or Scala already provide a Unit type, but Microsoft chose to prevent using the existing `System.Void` type in C# user code in favor of the `void` keyword.
## Usage
Use `Unit` as return value instead of the `void` keyword.
```csharp
// use
using TheVoid;
public Unit DoSomething() {
//...
}
// instead of
public void DoSomething() {
//...
}
```
To "return" Unit simply use one of the following
* `Unit.Default` *(recommended)*
* `default(Unit)`
* `new Unit()`
## Similar projects
* [@gregoryyoung](https://github.com/gregoryyoung) created a similar type which he called `Nothing` but it missed some things.
* [@Reactive-Extensions](https://github.com/Reactive-Extensions/Rx.NET) the .net project contains a `System.Unit` type which is not available as a standalone package.
## Planned deprecation
This project is deprecated as soon as any Unit type is included in C#.
## Maintainer(s)
* [@bomret](https://github.com/bomret)