Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the value associated with the specified key. In C#, this property is the indexer.
public:
virtual property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ keyword); void set(System::String ^ keyword, System::Object ^ value); };
public override object this[string keyword] { get; set; }
member this.Item(string) : obj with get, set
Default Public Overrides Property Item(keyword As String) As Object
The key of the item to get or set.
The value associated with the specified key.
keyword
is a null reference (Nothing
in Visual Basic).
Tried to add a key that does not exist within the available keys.
Invalid value within the connection string (specifically, a Boolean or numeric value was expected but not supplied).
The following code, in a console application, creates a new SqlConnectionStringBuilder and adds key/value pairs to its connection string, using the Item[] property.
class Program
{
static void Main()
{
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["Integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks";
// Overwrite the existing value for the Data Source value.
builder["Data Source"] = ".";
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
}
Module Module1
Sub Main()
Dim builder As New SqlConnectionStringBuilder
builder.Item("Data Source") = "(local)"
' Item is the default property, so
' you needn't include it in the reference.
builder("Integrated Security") = True
builder.Item("Initial Catalog") = "AdventureWorks"
' Overwrite the existing value for the Data Source value.
builder.Item("Data Source") = "."
Console.WriteLine(builder.ConnectionString)
Console.WriteLine()
Console.WriteLine("Press Enter to continue.")
Console.ReadLine()
End Sub
End Module
Because the SqlConnectionStringBuilder contains a fixed-size dictionary, trying to add a key that does not exist within the dictionary throws a KeyNotFoundException.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided) |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided) |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in