Tracks
/
C#
C#
/
Syllabus
/
Defensive Copying
De

Defensive Copying in C#

1 exercise

About Defensive Copying

Imagine you have a code-base of several hundred thousand lines. You are passed the dictionary of developers into some method you are developing. Perhaps you have been tasked with printing out details of privileged developers. You decide to blank out the eye color in the dictionary to protect the developers' privacy. Unless a deep copy of the dictionary was made in the Authenticator.GetDevelopers() method, or, even better, it was wrapped in a read-only collection then you will have just trashed the authenticator.

This follows the principle of defensive copying. You can make sure your formal API is not circumvented by avoiding exposure to callers of internal writeable state.

Edit via GitHub The link opens in a new window or tab

Learn Defensive Copying