The requested page is not available for the requested platform. You are viewing the content for Default platform.

Convert to Constant

Converts the string variable or field under the cursor to a constant.

Purpose

Convert to Constant is great when you have a string variable or a field whose value never changes. Using constants makes your code safer.

Availability

Available from the context menu or via shortcuts:

  • when the cursor is on a string variable or field declaration with initialization, provided that its value is never changed.

Example

string _MyString = "MyString";
Private _MyString As String = "MyString"

Result:

const string _MyString = "MyString";
Private Const _MyString As String = "MyString"

Screenshot

CSharpConvertToConstant