Show / Hide Table of Contents

Class User

Attributes of a user for whom you are evaluating feature flags.

Inheritance
System.Object
User
Implements
System.IEquatable<User>
IJsonSerializable
Namespace: LaunchDarkly.Sdk
Assembly: LaunchDarkly.CommonSdk.dll
Syntax
public class User : Object, 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, Nullable<Boolean>, 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, Nullable<bool> anonymous, ImmutableDictionary<string, LdValue> custom, ImmutableHashSet<string> privateAttributeNames)
Parameters
Type Name Description
System.String key
System.String secondary
System.String ip
System.String country
System.String firstName
System.String lastName
System.String name
System.String avatar
System.String email
System.Nullable<System.Boolean> anonymous
System.Collections.Immutable.ImmutableDictionary<System.String, LdValue> custom
System.Collections.Immutable.ImmutableHashSet<System.String> privateAttributeNames

Properties

Anonymous

Whether or not the user is anonymous.

Declaration
public bool Anonymous { get; }
Property Value
Type Description
System.Boolean

Avatar

The user's avatar.

Declaration
public string Avatar { get; }
Property Value
Type Description
System.String

Country

The country code for the user.

Declaration
public string Country { get; }
Property Value
Type Description
System.String

Custom

Custom attributes for the user.

Declaration
public IImmutableDictionary<string, LdValue> Custom { get; }
Property Value
Type Description
System.Collections.Immutable.IImmutableDictionary<System.String, LdValue>

Email

The user's email address.

Declaration
public string Email { get; }
Property Value
Type Description
System.String

FirstName

The user's first name.

Declaration
public string FirstName { get; }
Property Value
Type Description
System.String

IPAddress

The IP address of the user.

Declaration
public string IPAddress { get; }
Property Value
Type Description
System.String

Key

The unique key for the user.

Declaration
public string Key { get; }
Property Value
Type Description
System.String

LastName

The user's last name.

Declaration
public string LastName { get; }
Property Value
Type Description
System.String

Name

The user's full name.

Declaration
public string Name { get; }
Property Value
Type Description
System.String

PrivateAttributeNames

Used internally to track which attributes are private.

Declaration
public IImmutableSet<string> PrivateAttributeNames { get; }
Property Value
Type Description
System.Collections.Immutable.IImmutableSet<System.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
System.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
System.Boolean

Equals(Object)

Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
System.Object obj
Returns
Type Description
System.Boolean

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
System.Int32

WithKey(String)

Creates a user with the given key.

Declaration
public static User WithKey(string key)
Parameters
Type Name Description
System.String key

a string that uniquely identifies a user

Returns
Type Description
User

a User instance

Implements

System.IEquatable<>
IJsonSerializable
In This Article
Back to top Generated by DocFX