String - ToSecureString

A String extension method that converts the string object to a secure string.

Try it

public static void Main()
    string input = "Mwasdf{123[456]678}";

	//C# Extension Method: String - ToSecureString
    var secureString = input.ToSecureString();

    Console.WriteLine("Length of a string: {0}", secureString.Length);

View Source
using System;
using System.Security;

public static partial class Extensions
    /// <summary>
    ///     A String extension method that converts the @this to a secure string.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>@this as a SecureString.</returns>
    public static SecureString ToSecureString(this string @this)
        var secureString = new SecureString();
        foreach (Char c in @this)

        return secureString;