Class User
Attributes of a user for whom you are evaluating feature flags.
Inherited Members
Namespace: LaunchDarkly.Sdk
Assembly: LaunchDarkly.CommonSdk.dll
Syntax
[JsonConverter(typeof(LdJsonConverters.UserConverter))]
public class User : IEquatable<User>, IJsonSerializable
Remarks
User contains any user-specific properties that may be used in feature flag configurations to produce different flag variations for different users. You may define these properties however you wish.
User supports only a subset of the behaviors that are available with the newer Context type. A User is equivalent to an individual Context that has a Kind of Default ("user"); it also has more constraints on attribute values than a Context does (for instance, built-in attributes such as Email can only have string values). Older LaunchDarkly SDKs only had the User model, and the User type has been retained for backward compatibility, but it may be removed in a future SDK version; also, the SDK will always convert a User to a Context internally, which has some overhead. Therefore, developers are recommended to migrate toward using Context.
The only mandatory property of User is the Key, which must uniquely identify each user. For authenticated users, this may be a username or e-mail address. For anonymous users, this could be an IP address or session ID.
Besides the mandatory key, User supports two kinds of optional attributes: built-in attributes (e.g. Name and Country) and custom attributes. The built-in attributes have specific allowed value types; also, two of them (Name and Anonymous) have special meanings in LaunchDarkly. Custom attributes have flexible value types, and can have any names that do not conflict with built-in attributes.
Both built-in attributes and custom attributes can be referenced in targeting rules, and are included in analytics data.
Instances of User
are immutable once created. They can be created with the factory method
WithKey(string), or using a builder pattern with Builder(string)
or Builder(User).
For converting this type to or from JSON, see LaunchDarkly.Sdk.Json.
Constructors
User(string, string, string, string, string, string, string, string, string, bool?, ImmutableDictionary<string, LdValue>, ImmutableHashSet<string>)
Creates a user by specifying all properties.
Declaration
public User(string key, string secondary, string ip, string country, string firstName, string lastName, string name, string avatar, string email, bool? anonymous, ImmutableDictionary<string, LdValue> custom, ImmutableHashSet<string> privateAttributeNames)
Parameters
Type | Name | Description |
---|---|---|
string | key | |
string | secondary | |
string | ip | |
string | country | |
string | firstName | |
string | lastName | |
string | name | |
string | avatar | |
string | ||
bool? | anonymous | |
ImmutableDictionary<string, LdValue> | custom | |
ImmutableHashSet<string> | privateAttributeNames |
Properties
Anonymous
Whether or not the user is anonymous.
Declaration
public bool Anonymous { get; }
Property Value
Type | Description |
---|---|
bool |
Avatar
The user's avatar.
Declaration
public string Avatar { get; }
Property Value
Type | Description |
---|---|
string |
Country
The country code for the user.
Declaration
public string Country { get; }
Property Value
Type | Description |
---|---|
string |
Custom
Custom attributes for the user.
Declaration
public IImmutableDictionary<string, LdValue> Custom { get; }
Property Value
Type | Description |
---|---|
IImmutableDictionary<string, LdValue> |
The user's email address.
Declaration
public string Email { get; }
Property Value
Type | Description |
---|---|
string |
FirstName
The user's first name.
Declaration
public string FirstName { get; }
Property Value
Type | Description |
---|---|
string |
IPAddress
The IP address of the user.
Declaration
public string IPAddress { get; }
Property Value
Type | Description |
---|---|
string |
Key
The unique key for the user.
Declaration
public string Key { get; }
Property Value
Type | Description |
---|---|
string |
LastName
The user's last name.
Declaration
public string LastName { get; }
Property Value
Type | Description |
---|---|
string |
Name
The user's full name.
Declaration
public string Name { get; }
Property Value
Type | Description |
---|---|
string |
PrivateAttributeNames
Used internally to track which attributes are private.
Declaration
public IImmutableSet<string> PrivateAttributeNames { get; }
Property Value
Type | Description |
---|---|
IImmutableSet<string> |
Methods
Builder(User)
Creates an IUserBuilder for constructing a user object, with its initial properties copied from an existeing user.
Declaration
public static IUserBuilder Builder(User fromUser)
Parameters
Type | Name | Description |
---|---|---|
User | fromUser | the user to copy |
Returns
Type | Description |
---|---|
IUserBuilder | a builder object |
Remarks
This is the same as calling User.Builder(fromUser.Key)
and then calling the
IUserBuilder methods to set each of the individual properties from their current
values in fromUser
. Modifying the builder does not affect the original User.
Examples
var user1 = User.Builder("my-key").FirstName("Joe").LastName("Schmoe").Build();
var user2 = User.Builder(user1).FirstName("Jane").Build();
// this is equvalent to: user2 = User.Builder("my-key").FirstName("Jane").LastName("Schmoe").Build();
Builder(string)
Creates an IUserBuilder for constructing a user object using a fluent syntax.
Declaration
public static IUserBuilder Builder(string key)
Parameters
Type | Name | Description |
---|---|---|
string | key | a string that uniquely identifies a user |
Returns
Type | Description |
---|---|
IUserBuilder | a builder object |
Remarks
This is the only method for building a User if you are setting properties besides the Key. The IUserBuilder has methods for setting any number of properties, after which you call Build() to get the resulting User instance.
Examples
var user = User.Builder("my-key").Name("Bob").Email("test@example.com").Build();
Equals(User)
Declaration
public bool Equals(User u)
Parameters
Type | Name | Description |
---|---|---|
User | u |
Returns
Type | Description |
---|---|
bool |
Equals(object)
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj |
Returns
Type | Description |
---|---|
bool |
Overrides
GetAttribute(UserAttribute)
Gets the value of a user attribute, if present.
Declaration
public LdValue GetAttribute(UserAttribute attribute)
Parameters
Type | Name | Description |
---|---|---|
UserAttribute | attribute | the attribute to get |
Returns
Type | Description |
---|---|
LdValue | the attribute value or Null |
Remarks
This can be either a built-in attribute or a custom one. It returns the value using the LdValue type, which can have any type that is supported in JSON. If the attribute does not exist, it returns Null.
GetHashCode()
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int |
Overrides
WithKey(string)
Creates a user with the given key.
Declaration
public static User WithKey(string key)
Parameters
Type | Name | Description |
---|---|---|
string | key | a string that uniquely identifies a user |
Returns
Type | Description |
---|---|
User | a User instance |