Converting Hashtables to Generic Dictionaries
When using a dictionary you can do the following:
Hashtable
hashtable = new Hashtable();string test;
test = (string)hashtable["a"]; // test is “a”
test = (string)hashtable["c"]; // test is null
However, when converting this to a generic dictionary, it has a different expected behaviour:
Dictionary
<string, string> dictionary = new Dictionary<string, string>();string test;
test = dictionary["a"]; // test is “a”
test = dictionary["c"]; // throws KeyNotFoundException’
You can either do this:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
string test;
try
{
test = dictionary["c"]; // throws KeyNotFoundException’
}
catch
{
test = null;
}
or use the more generic approach:
dictionary.TryGetValue( "c", out test ); // test is null
If the key is not found, then the value parameter gets the appropriate default value for the value type V;
for example, zero (0) for integer types, false for Boolean types, and null for reference types.
Recent Posts
- Understanding API First Strategy and Benefits
- Frankenstein APIs Explained! - API Cyber Security Series
- API Security 101 - Cyber Security Explained
- API Trends 2022 - API Security and Cybersecurity
- API Trends 2022 - Seamless Integration Solutions
- API Trends 2022 - Adaptive API Management
- API Trends 2022 - API Integration Automation
- API Trends 2022 - Industry Specific Breakouts
- API Trends 2022 - API Best Practices
- API Trends 2022 - Open API Standards
- API Trends 2022 - API Integration Experience
- API Trends 2022 - API-Led Modernization
- API Trends 2022 - API Economy Growth
- Brenton House - Give your App and APIs a Turbo Boost – Part 2
- Easily Enable Speech Recognition in Titanium iOS using Hyperloop
Tag Cloud
- .NET
- adaptive api management
- android
- api
- api automation
- api best practices
- api economy
- api experience
- api first
- api integration
- api integrations
- api management
- api security
- api standards
- api strategy
- api trends
- apis
- appcelerator
- automation
- boot
- Brenton House
- C#
- Cisco VPN
- Cloud
- Community News
- Continuous Integration
- crash
- cybersecurity
- Dropbox
- fhir apis
- Free Stuff
- General Software Development
- Google Drive
- graphql
- healthcare apis
- houserules
- hyperloop
- ios
- json schema
- Live Mesh
- Mac OS X
- Microsoft
- mobile
- mobile api
- native
- open banking
- openapi
- Other Stuff
- SkyDrive
- speech
- Stuff
- swagger
- Team Systems
- titanium native
- titanium turbo
- Unit Testing
- Vista
- Visual Studio
- Visual Studio 11
- voice
- win7
- Windows 7
- Windows 8
- windows update