As its name suggests, its main purpose is to provide security
. Normally the strings/texts with sensitive information (like credit cards, passwords, etc.) that should be kept confidential are stored in SecureString variable. This string gets deleted from computer memory when no longer needed. The value of an instance of SecureString is automatically protected using a mechanism supported by the underlying platform when the instance is initialized or when the value is modified.
A SecureString object is similar to a String object in that it has a text value. However, the major difference is as follows-
String
It is not possible to predict when an instance of the System.String class will be deleted from computer memory. So, if a String object contains sensitive information, there is a risk the information could be revealed after it is used because your application cannot delete the data from computer memory.
SecureString
The value of a SecureString object is pinned in memory and it automatically provides encryption, ability to mark as read-only, safe construction by NOT allowing a constant string to be passed in