Revisiting users logged in to a service
In Chapter 2, Arrays: Foundational Structures, we created an app to keep track of users logged into a web service, using an array as the underlying data structure containing the User
objects. However, this design can be greatly improved upon by using a list data structure. Let's revisit the users logged into a service problem here, and by replacing the class array with a list, we will see that our original code is both abbreviated and more readable in most cases.
C#
In this example, we have replaced the User[]
object with a List<User>
object. Much of this refactor is obvious, but three lines of code should be noted. First, in the CanAddUser()
method, we have replaced 15 lines of code with 2 lines of code by leveraging the List<T>.Contains()
method and condensing our logic loop. Next, in the UserAuthenticated()
method, we have leveraged the List<T>.Add()
method, which replaced the call to Array.Resize()
and the error-prone approach to...