Class User
Attributes of a user for whom you are evaluating feature flags.
Inheritance
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.
The only mandatory property 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: interpreted attributes (e.g. IPAddress and Country) and custom attributes. LaunchDarkly can parse interpreted attributes and attach meaning to them. For example, from an IPAddress, LaunchDarkly can do a geo IP lookup and determine the user's country.
Custom attributes are not parsed by LaunchDarkly. They can be used in custom rules-- for example, a custom attribute such as "customer_ranking" can be used to launch a feature to the top 10% of users on a site. Custom attributes can have values of any type supported by JSON.
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 | ||
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 |
AnonymousOptional
Whether or not the user is anonymous, if that has been specified.
Declaration
public Nullable<bool> AnonymousOptional { get; }
Property Value
Type | Description |
---|---|
System.Nullable<System.Boolean> |
Remarks
Although the Anonymous property defaults to false in terms of LaunchDarkly's user indexing behavior, for historical reasons null (the property has not been explicitly set) may behave differently from being explicitly set to false, if this property is referenced in a feature flag rule. This property getter, and the corresponding setter in IUserBuilder, allow you to treat the property as nullable.
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> |
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> |
Secondary
The secondary key for a user, which can be used in feature flag targeting.
Declaration
public string Secondary { get; }
Property Value
Type | Description |
---|---|
System.String |
Remarks
The use of the secondary key in targeting is as follows: if you have chosen to bucket users by a specific attribute, the secondary key (if set) is used to further distinguish between users who are otherwise identical according to that attribute.
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 |