Updated April 3, 2023
Introduction to C# Object to String
In C# object is the root class and it is represented with the block of memory that has been used for allocating the user datas and configured to the other default keywords and functions that can be associated with the classes if we want to convert object to string types by using the default method like Object.toString() mainly it converts the object to its string representations so that it is used for validating the user inputs the object will be created with the help of constructor it may be derived or parent classes the object is to be created by using Object.toString() method it returns the string as the result.
Syntax:
In C# object is the main class and it’s also the parent or root of all the classes hence c# is the object-oriented language. We can convert the object to other data types like string, arrays, etc. Here we convert the object to a string by using the Object.toString() method for translating the object to string values.
datatype[] reference or variable name={};
Any package classes reference name1 = new class name();
reference name1.ToString();
—some code logics depends upon the user requirement—–
The above code is one of the basic syntax for utilizing and creating the object for the specific class and it is converted to the string data type by using the default method like Object.ToString().
How Object to String function work in C#
The Object.toString() is one of the main methods in the C#. net framework technology mainly it converts any object to string representation so it is more ever need for to display the object to string values. In default, the implementation team will implement the ToString method it always returns the value in the string type according to the specific object which we have created already in the code. Generally, the Object is the base class for all other reference types in the framework this default behavior is inherited by the specific reference types it cannot override the ToString method in the application. Basically, the string represents the current object of the method which is to be declared on the classes. We can also override this method whenever we used it on a specific area of the code also it always returns the string that represents and create the object instance whenever necessary for the application. The data types like Char, Int32, float, double, etc these are the other datatypes that have to be converted the string by using the toString() method. Likethat we can overload the ToString() method that can provide the version of the function it accepts all the support parameters in the application classes.
Examples
Let us discuss examples of C# object to string.
Example #1
Code:
using System;
using demo;
namespace demo
{
public class demo1
{
public void exam()
{
Console.WriteLine("Welcome To My Domain its the first example for C# programming language we want to convert the object to string values");
}
}
}
public class demo2
{
public static void Main()
{
object firstobj = new demo1();
Console.WriteLine(firstobj.ToString());
}
}
Sample Output:
In the above example, we used ToString() method in basic logic that is we can create two classes and has one method. We create the object and covert it by using ToString() method.
Example #2
Code:
using System;
public class demo
{
private int integers;
private string stringsfirst;
private int secondinteger;
private string secondstrings;
private string thirdstrings;
public demo(string str1, int intr , int intr1,
string str2, string str3)
{
secondstrings = str1;
secondinteger = intr;
integers = intr1;
stringsfirst = str2;
thirdstrings = str3;
}
public int first
{ get { return integers; } }
public string second
{ get { return secondstrings; } }
public int third
{ get { return secondinteger; } }
public string four
{ get { return stringsfirst; } }
public string five
{ get { return thirdstrings; } }
public override string ToString()
{
return ToString("A");
}
public string ToString(string vars)
{
if (string.IsNullOrEmpty(vars))
vars = "A";
switch (vars.ToUpperInvariant())
{
case "A":
return string.Format("Welcome To My Domain its a second example for the toString() method to 1, 4 convert the other data types to string values please provide your inputs which depends upon the data types need on the user requirement", secondinteger, secondstrings);
case "B":
return string.Format("second string depends upon user inputs",
secondinteger, secondstrings, integers);
case "C":
return string.Format("Welcome To My Domain its a third example for the toString() method to 1, 4 convert the other data types to string values please provide your inputs which depends upon the data types need on the user requirement",
secondinteger, secondstrings, stringsfirst);
case "D":
return string.Format("Welcome To My Domain its a four example for the toString() method to 1, 4 convert the other data types to string values please provide your inputs which depends upon the data types need on the user requirement",
secondinteger, secondstrings, integers, stringsfirst);
case "E":
return string.Format("welcome To My Domain fifth strings",
secondinteger, secondstrings, integers, stringsfirst);
default:
string res = string.Format("Please enter valid input values depends upon the data type fomats based on the user requirements",
vars);
throw new ArgumentException(res);
}
}
}
public class FinalClasses
{
public static void Main()
{
var refr = new demo("First input is the string", 621435, 81275, "Fourth input also strings", "fifth strings");
Console.WriteLine(refr.ToString("B"));
Console.WriteLine(refr.ToString("E"));
}
}
Sample Output:
In the second example, we used the same logic as discussed in the previous example but here we used additional concepts like overriding the class and its methods. It’s not affected in ToString() method when we want to apply the overriding logic in the code.
Example #3
Code:
using System;
using System.Collections.Generic;
public class Example<T> : List<T>
{
public Example(IEnumerable<T> demo1) : base(demo1)
{ }
public Example() : base()
{}
public override string ToString()
{
string results = string.Empty;
foreach (T vars1 in this) {
if (string.IsNullOrEmpty(results)){
results += vars1.ToString();
Console.WriteLine("Conditions checking");
}
else{
results += string.Format("Third Example", vars1);
Console.WriteLine(results);
}
}
return results;
}
}
public class Example
{
public static void Main()
{
var vars1 = new Example<int>();
vars1.Add(9284367);
Console.WriteLine(vars1.ToString());
}
}
Sample Output:
In the final example, we implement some default classes like collection packages with the help of class reference we can add the values in the array list memory. After that by using ToString() method the values are converted to string type.
Conclusion
In C# language everything is implemented by using the object class. But wherever we need to convert the types we use some default methods like ToString(), arrays, etc. Depending upon the user requirement the logic and methods are used in the code. In these articles we used ToString() method for converting the other data type values to string types.
Recommended Articles
This is a guide to C# Object to String. Here we discuss the Introduction, How Object to String function work in C#, and their Syntax and Examples. You may also have a look at the following articles to learn more –