LkNic 1.0.0

dotnet add package LkNic --version 1.0.0
                    
NuGet\Install-Package LkNic -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LkNic" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LkNic" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="LkNic" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LkNic --version 1.0.0
                    
#r "nuget: LkNic, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LkNic@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LkNic&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=LkNic&version=1.0.0
                    
Install as a Cake Tool

<p align="center"> <img src="LkNicIcon.png" width="128" alt="LkNic Logo" /> </p>

LkNic

NuGet Version NuGet Downloads License Build and Test

A .NET library for parsing, validating, and converting Sri Lankan National Identity Card (NIC) numbers. It supports both the legacy (9-digit + V/X) and modern (12-digit) formats.

Table of Contents

Why choose LkNic?

  • Lightweight
  • Zero runtime dependencies
  • Strongly typed API
  • Supports old and new NIC formats

Features

  • Validates digit counts, formats, and leap-year birth dates.
  • Extracts birth year, birth date, age, gender, and day of year.
  • Normalizes inputs by removing spaces and hyphens, and converting letters to uppercase.
  • Converts legacy 9-digit NICs to the 12-digit format.

Supported Formats

Format Pattern Example
Modern 12 digits 198554200159
Legacy 9 digits + V or X 855420159V

Compatibility

Framework Support
.NET 10 ✅
.NET 9 ✅
.NET 8 ✅

Installation

dotnet add package LkNic

Quick Start

Parse a NIC string to easily access its properties:

using LkNic;

var nic = Nic.Parse("198554200159");

Console.WriteLine(nic.BirthYear);  // 1985
Console.WriteLine(nic.BirthDate);  // 1985-02-11
Console.WriteLine(nic.Gender);     // Female
Console.WriteLine(nic.AgeInYears); // 41

API Overview

  • Nic.Parse(string): Parses a NIC string and throws an exception on failure.
  • Nic.TryParse(string, out Nic): Attempts to parse a NIC string safely without throwing exceptions.
  • Nic.IsValid(string): Returns a boolean indicating whether a NIC string is valid.
  • ToNewFormat(): Returns the 12-digit representation of the NIC.

Usage

Simple Validation

If you only need to verify whether an input is a valid NIC without extracting data:

bool isValid = Nic.IsValid("198554200159");

Validation and Error Handling

Use Nic.TryParse to validate user input safely and handle specific errors:

if (!Nic.TryParse("85542-0159-v", out var parsedNic, out var error))
{
    Console.WriteLine($"Invalid NIC: {error}");
}
else
{
    Console.WriteLine(parsedNic.Value); // 855420159V
}

Format Conversion

Standardize legacy NIC formats for database storage:

var oldNic = Nic.Parse("855420159V");
string standardized = oldNic.ToNewFormat(); 

Console.WriteLine(standardized); // 198554200159

Nic Properties

The Nic model exposes the following properties:

public class Nic
{
    public string Value { get; }
    public NicType Type { get; }
    public int BirthYear { get; }
    public DateOnly BirthDate { get; }
    public Gender Gender { get; }
    public int DayOfYear { get; }
    public int AgeInYears { get; }
}

Disclaimer

This library is an independent, open-source project and is not affiliated with, endorsed by, or connected to the Department of Registration of Persons (DRP), the Government of Sri Lanka, or any other government institution. The author(s) and contributors bear no responsibility or liability for any errors, omissions, or consequences arising from the use of this library in any application, legal proceeding, or business operation. Use it at your own risk.

Contributing

Contributions are welcome. Feel free to open issues and submit pull requests.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 145 7/25/2026
0.1.0-alpha.1 79 7/12/2026