Smashery
Smashery

Reputation: 59733

Use of enums in API

I'm designing an API in C#, and one function I need is something along the lines of:

void CreateChild(string name, ChildType type);

Where ChildType is defined as:

public enum ChildType { Number = 0, Text = 1 };

It should be fairly self-explanatory, but essentially, it will create a child of the object in question, and the type of value that child contains will either be a string or a double based on the value of the enum.

The ChildType enum is public, thus, I assume, would be accessible to users of the API.

My concern is that some languages won't understand the enum in the signature and won't be able to program to this API. Is this something to be concerned about, or are my fears unfounded?

Upvotes: 0

Views: 1445

Answers (2)

Jeffrey L Whitledge
Jeffrey L Whitledge

Reputation: 59553

The base class library of the .NET Framework itself has many methods that use enums as parameters (e.g., string.Equals(string, StringComparison)). The main .NET languages certainly understand them. Any language that doesn't have enums could potentially simulate them with named constants.

I suspect that using an enum in a public API would not be a problem for other languages.

Upvotes: 0

Mike Ruhlin
Mike Ruhlin

Reputation: 3556

As long as you have good mappings of the integer values to what they mean, you should be in the clear. Any languages that don't have an enum concept can still refer to it by the integer value.

Be cautious about using the [Flags] attribute, since it kind of convolutes the definition of an enum. We auto-generate an SDK in several languages including Java, and had to scramble a bit when we realized that some of our properties were [Flags], but being mapped to regular enums on Java's end, so Java didn't understand combined values.

Upvotes: 2

Related Questions