Initializing a String object in C#
In two ways you can initialize string object.
1-By using Keyword
2-By using String Class
1-Initializing a String object using Keywords
Declare a string keyword name first and then initialize a string by calling string keyword name, using functions.so let's see an example which shows to initialize a String as follows.
Program
using System;
namespace program
{
class Program
{
static void Main(string[] args)
{
string str1 = "Hello";
Console.WriteLine(str1);
Console.ReadKey();
}
}
}
Output
Hello
2-Initializing a String object using String Class
Now we will see an example to initialize a string using String Class as follows.
Program
using System;
public class String
{
public static void Main(string[] args)
{
char[] chr = { 'H', 'e', 'l', 'l', 'o' };
string str1 = new string(chr);
Console.WriteLine(str1);
Console.ReadKey();
}
}
Output
Hello
In two ways you can initialize string object.
1-By using Keyword
2-By using String Class
1-Initializing a String object using Keywords
Declare a string keyword name first and then initialize a string by calling string keyword name, using functions.so let's see an example which shows to initialize a String as follows.
Program
using System;
namespace program
{
class Program
{
static void Main(string[] args)
{
string str1 = "Hello";
Console.WriteLine(str1);
Console.ReadKey();
}
}
}
Output
Hello
2-Initializing a String object using String Class
Now we will see an example to initialize a string using String Class as follows.
Program
using System;
public class String
{
public static void Main(string[] args)
{
char[] chr = { 'H', 'e', 'l', 'l', 'o' };
string str1 = new string(chr);
Console.WriteLine(str1);
Console.ReadKey();
}
}
Output
Hello