A .NET library for working with Swedish identity numbers.
The SwedishIdentityNumbers
library provides a set of classes for working with Swedish identity numbers:
Personnummer
for personal identity numbersSamordningsnummer
for coordination numbersOrganisationsnummer
for organization numbers
Each of these classes inherits from the abstract base class SwedishIdentityNumber
, which provides common functionality for validating and working with identity numbers.
Install SwedishIdentityNumbers
via NuGet:
dotnet add package SwedishIdentityNumbers
Or search for SwedishIdentityNumbers
in the NuGet package manager in Visual Studio.
using SwedishIdentityNumbers;
var personnummer = new Personnummer("8507301234"); // also allows 850730-1234
// throws
// ArgumentNullException if null
// ArgumentException if empty
// FormatException if not of the correct format
// ValidationException if the check digit is wrong
var dateOfBirth = personnummer.DateOfBirth;
var legalSex = personnummer.LegalSex;
using SwedishIdentityNumbers;
if (Personnummer.TryCreate("8507301234", out var personnummer)) // also allows 850730-1234
{
// Successfully created
}
using SwedishIdentityNumbers;
var samordningsnummer = new Samordningsnummer("8507901234"); // also allows 850790-1234
// throws
// ArgumentNullException if null
// ArgumentException if empty
// FormatException if not of the correct format
// ValidationException if the check digit is wrong
var dateOfBirth = samordningsnummer.DateOfBirth;
var legalSex = samordningsnummer.LegalSex;
using SwedishIdentityNumbers;
var organisationsnummer = new Organisationsnummer("5560360793"); // also allows 165560360793 and 556036-0793
// throws
// ArgumentNullException if null
// ArgumentException if empty
// FormatException if not of the correct format
// ValidationException if the check digit is wrong
var companyForm = organisationsnummer.ProbableSwedishCompanyForm;
The library includes a SwedishCompanyForm
enum, representing different forms of companies in Sweden. This enum is used in conjunction with the Organisationsnummer
class to identify the probable form of a company based on its organization number. The different enum values represent various types of company forms, ranging from Joint Stock Companies to Government Agencies. The implementation and enum values are based on information from Bolagsverket.
public enum SwedishCompanyForm
{
JointStockCompany, // Aktiebolag, filialer, banker, försäkringsbolag och europabolag
GeneralPartnership, // Handelsbolag och kommanditbolag
HousingCooperative, // Bostadsrättsföreningar, ekonomiska föreningar, etc.
ReligiousCommunity, // Trossamfund
GovernmentAgency, // Statlig myndighet
Unknown // Okänd
}
To work on SwedishIdentityNumbers
, clone the repo and open SwedishIdentityNumbers.sln
in Visual Studio or your preferred editor.
Run the tests in SwedishIdentityNumbers.Tests
to ensure everything is working correctly.
SwedishIdentityNumbers
is licensed under the MIT License. See the LICENSE file for details.