CSharp - Write program to Store text in string type variable


Perform some basic operations with variable.

  • You'll create a variable named message.
  • Afterward, you will store some text in it.
  • Finally, you will display the value of the variable to the user.


string is the data type.

Put text value into double quote


using System;

class Program/*from   w ww .  j  a  v a  2s .  c  o m*/
    static void Main(string[] args)
        // Declaration of a variable to store text 
        string message;

        // Storing a value in prepared variable (assignment statement) 
        message = "this is a test.";

        // Another variable
        string anotherMessage = "from book2s.com";

        // Output of variables 


Variable Declaration

To use a variable, to declare it first.

The general syntax of a variable declaration statement is as follows:

typeName  variableName; 

In this case, it reads as follows:

string message; 

The type denotes the category of values to store in the variable.

In this case, the value is text, which is why you used the type called string.

You can do the declaration and assignment together.

Here is an example of this syntax:

string anotherMessage = "your value"; 

Assignment Statement

The second statement is as follows:

message = "this is a test"; 

This stores a value (the text "this is a test") in the prepared variable (message).

It is called an assignment statement.

You use it whenever you want to store something.

The general syntax of the assignment statement is as follows:

variable = your value;