c sharp

C# tostring

In every programming language, we need to change the data type of a variable at times. Switching from one data type to a string is challenging in some languages, but it is simple with C#. The string format is a fully qualified identifier of the object’s type, followed by the namespace (if any) where its class is declared.  The ToString() function is a method of the Object class, and it is the parent class of all classes in C#, it is available to all classes by default, like the other methods of the Object class. The ToString method in C# will be explained in detail in this article.

What is ToString Function in C# in Ubuntu 20.04?

The ToString() function in C# converts a string representation of an object. When an object is displayed, the ToString() function is used implicitly. However, we have to invoke the ToString() function of any object explicitly. All referencing object types have the ToString() function.  This is the case where all reference types are inherited from Object, which provides the ToString() method. The type name is returned by the object base class’s ToString() function.

Syntax of the ToString Function in C# in Ubuntu 20.04

The general syntax of the ToString function we use in C# is given below:

Public string ToString();

We have to invoke the ToString function with the data type and it passes no parameter in it. The ToString function returns the collection as a String.

How to use ToString Function in C# in Ubuntu 20.04

To understand how we use the ToString function in C# programming language, we should take a look at the examples given below:

Example 1: Using the ToString function in C# in Ubuntu 20.04

The following example program is a simple ToString function. The string data type is used for the ToString function and also the integer data type used ToString function.

Initially, we have imported the system library “using System” of C# that will allow us to call the C# function and methods required in the program. After that, the class is created with the access specifier keyword “public”. The class is represented with the name “MyProgram” and the class is called the main function. In the main function, we have declared a variable as “str1” of string data type and assigned it a string value.

Then another variable is declared as “int1” of integer data type and assigned a numeric value. The ToString function is used here for the variables “str1” and “int1” and called in new variables “MyStr1” and “MyStr2”. These variables are passed in a C# writeLine method which will print the returned values from these variables.

The output from the ToString function used for different data types is shown in the console screen below:

Example 2: Using the ToString function for numeric Format in C# in Ubuntu 20.04

The GetHash method is used in the program below which will get the value of the ToString function. The String contains the numeric format string values below:

The system library is used here as “using System” and the class is defined as “DemoProgram”. The class block is called the main function as static void main(). In the main function, we have the variable declarations which are represented as “value1” and “value2”. The variables are set to a short data type that occupies 2 bytes (16 bits) and assigns a numeric value. This instance’s numeric value is converted to its corresponding string representation using the C# Int16.ToString() function. The Int16 is an alias for short keywords. The ToString function is called in the c# writeLine method for the variables “value1” and “value2”.

After converting the variables in the ToString function, we have used the GetHashCode method which returns the hash code in numeric value of the variables “value1” and “value2”.The hash code provided by the GetHashCode method for the algorithms promotes fast object equality tests. In the last writeLine method, we have invoked an equal function that checks if the hash codes of the “value1” and “value2” are equal or not and returns the Boolean value.

The output of the ToString function and the hash code numeric value are displayed in the image below along with the Boolean value from the equals function:

Example 3: Using Stack ToString function in C# in Ubuntu 20.04

The GetType() function is used in the following program to get the current object’s type. It will show whether or not the specified Stack object has been converted to a string:

We have imported two system libraries “using System” and “using system collections” of C# as required for the code. First, the class is defined with the name “Demo” and then called the main function of the class. In the main function, we built an empty stack of the object “st1.Then, we have used the push() method through which we have added the elements in the empty stack. The GetType() function is invoked in the writeLine method to get the type of “st1”.After this, we have a foreach loop for assigning the st1 to the Str1 using the “in” keyword. In the block of the foreach loop, we have the ToString function for “str1”. Now, we have used the GetType method to get the “str1” type in the witeLine method.

The output we get from executing the program above is shown on the terminal screen:

Example 4: Using Stack ToString function for int in C# in Ubuntu 20.04

There is another program below where we illustrated the stack ToString function for the integer data type.

After importing the required system libraries of C#, we have created a class and mentioned it as the “example”. The main function is called for the class “example” and in the main function we have created first an empty stack and declared it in a stack object “stk”. Then we have a push method for adding the elements to the empty stack. We have added the four integer values in the stack. The GetType method is used in the writeLine method to get the type of the “stk”. The foreach loop was used which created an object “a” of integer type and got the elements from “stk”. The Tostring method is used for “Stk” along with the GetType method.

The results are shown in the following image:

Example 5: Using the ToString function for Date Time format in C# in Ubuntu 20.04

A DateTime is a date object which is converted to string format using the ToString() function of the DateTime class. The method accepts a date format string that defines the string representation that must be used.

Here we have a class “MyDemo” in which its main function is called. The main function has a DateTime class which declares its object as “MyDate”. We have set the data time as “2022-4-4 21:30:12.004” with the “new” keyword for class “DateTime”. Then we have a variable “dateStr” in which we have used ToString for converting DateTime to string format. The writeLine method will print the string format of DateTime.

The DateTime in string format is shown on the screen.

Conclusion

We have discussed all the possible ways to show how we can use the ToString function in the C# programming language. First, we have the definition of the ToString function and the general syntax we use in the code. Then we moved to the examples of the ToString function and used it with the different data types. We have examples of ToString DateTime too which convert the date time to string format. You will find ease while going through the ToString function here.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.