C# में String Object Create करने के लिए, नीचे दिए गए किसी भी Method का उपयोग करें।
- String Literal को String Variable में Assign करके
- String Class Constructor का उपयोग करके
- String Concatenation Operator (+) का उपयोग करके
- Property Retrieve करके या Method Call करके जो String Return करता है
- Formatting Method Call करके Value या Object को String Representation में Convert करने के लिए
निम्न Example C# में String Object Create करने के विभिन्न तरीकों को दर्शाता है।
Example
using System; namespace Demo { class Program { static void Main(string[] args) { // String Literal और String Concatenation से string fname, lname; fname = "Brad "; lname = "Pitt"; char[] letters = { 'W', 'e', 'b'}; string[] sarray = { "Web", "World"}; string fullname = fname + lname; Console.WriteLine("Full Name: {0}", fullname); // String Constructor का उपयोग करके { 'W', 'e', 'b'}; string greetings = new string(letters); Console.WriteLine("Greetings: {0}", greetings); // Methods Returning String { "Web", "World" }; string message = String.Join(" ", sarray); Console.WriteLine("Message: {0}", message); // Formatting Method Value Convert करने के लिए DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1); string chat = String.Format("Message sent at {0:t} on {0:D}", waiting); Console.WriteLine("Message: {0}", chat); } } }Output
Full Name: Brad Pitt Greetings: Web Message: Web World Message: Message sent at 5:58 PM on Wednesday, October 10, 2012