© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
4 replies
Core

✅ Best approach for returning typed objects

Hello,
I am porting a popular UA parser library called DeviceDetector to .NET.
The library determines the client from the user agent, which can be a browser, media player, feed reader, etc.

With the current implementation, there is an interface that all client models implement, and the returned object after parsing is of type
IClientInfo
IClientInfo
. This means that users would need to perform type checking and casting to determine the exact client type. As I see it, there are two possibilities, each providing the user with a different way of handling the concrete client.

Which of these approaches would be the best to follow?

1st approach
public interface IClientInfo
{
    string Name { get; init; }
    string? Version { get; init; }
}

public sealed class BrowserInfo : IClientInfo
{
    public string Name { get; init; }
    public string? Version { get; init; }
    public BrowserCode Code { get; init; }
    public string? Family { get; init; }
    public string? Engine { get; init; }
    public string? EngineVersion { get; init; }
}


// handling client after parsing
var result = DeviceDetector.Parse(userAgent);

if (result.Client is BrowserInfo browser)
{
  
}
public interface IClientInfo
{
    string Name { get; init; }
    string? Version { get; init; }
}

public sealed class BrowserInfo : IClientInfo
{
    public string Name { get; init; }
    public string? Version { get; init; }
    public BrowserCode Code { get; init; }
    public string? Family { get; init; }
    public string? Engine { get; init; }
    public string? EngineVersion { get; init; }
}


// handling client after parsing
var result = DeviceDetector.Parse(userAgent);

if (result.Client is BrowserInfo browser)
{
  
}


2nd approach - it's similar to the 1st approach, but it is extended with an enum that represents the concrete type
public interface IClientInfo
{
    ClientType Type {get; init; }
    string Name { get; init; }
    string? Version { get; init; }
}

public sealed class BrowserInfo : IClientInfo
{
    public ClientType Type {get; init; }
    public string Name { get; init; }
    public string? Version { get; init; }
    public BrowserCode Code { get; init; }
    public string? Family { get; init; }
    public string? Engine { get; init; }
    public string? EngineVersion { get; init; }
}

var result = DeviceDetector.Parse(userAgent);

// handling client after parsing
if (result.Client.Type == ClientType.Browser)
{
  var browser = (BrowserInfo)result.Client;
}
public interface IClientInfo
{
    ClientType Type {get; init; }
    string Name { get; init; }
    string? Version { get; init; }
}

public sealed class BrowserInfo : IClientInfo
{
    public ClientType Type {get; init; }
    public string Name { get; init; }
    public string? Version { get; init; }
    public BrowserCode Code { get; init; }
    public string? Family { get; init; }
    public string? Engine { get; init; }
    public string? EngineVersion { get; init; }
}

var result = DeviceDetector.Parse(userAgent);

// handling client after parsing
if (result.Client.Type == ClientType.Browser)
{
  var browser = (BrowserInfo)result.Client;
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Best container for ~1 million objects
C#CC# / help
2y ago
Best approach to SetThreadExecutionState
C#CC# / help
3y ago
❔ best practices for updating objects using GroupBy
C#CC# / help
3y ago
✅ Best approach for auth | ASP NET Web Api
C#CC# / help
14mo ago