Skip to content

Commit

Permalink
Merge pull request #447 from joelverhagen/tryadd
Browse files Browse the repository at this point in the history
Add TryAdd method to Dictionary
  • Loading branch information
yanghuan authored Dec 28, 2023
2 parents c8867b9 + 70207c4 commit f581a50
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/Dictionary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ local Dictionary = {
end
return remove(this, key)
end,
TryAdd = function (this, key, value)
if key == nil then throw(ArgumentNullException("key")) end
local exists, currentValue = this:TryGetValue(key)
if exists then
return false
end
this:set(key, value)
return true
end,
TryGetValue = function (this, key)
if key == nil then throw(ArgumentNullException("key")) end
local value = this[key]
Expand Down Expand Up @@ -633,6 +642,15 @@ local ArrayDictionary = (function ()
end
return false
end,
TryAdd = function (this, key, value)
if key == nil then throw(ArgumentNullException("key")) end
local exists, currentValue = this:TryGetValue(key)
if exists then
return false
end
this:set(key, value)
return true
end,
TryGetValue = function (this, key)
if key == nil then throw(ArgumentNullException("key")) end
local len = #this
Expand Down

0 comments on commit f581a50

Please sign in to comment.