Found 34487 Articles for Programming

The nameof keyword in C#

George John
Updated on 22-Jun-2020 13:47:39

488 Views

The nameof operator returns a string literal of an element that can be a variable, type or member.For example, the following is our variable −var vehicle = "motorbike";To get the string literal, use nameof −nameof(vehicle);The following is the code to implement nameof keyword −Example Live Demousing System; public class Program {    static void Main() {       var vehicle = "motorbike";       Console.WriteLine(nameof(vehicle));       var time = DateTime.Now.ToLocalTime();       Console.WriteLine(nameof(time));       var a = false;       Console.WriteLine(nameof(a));    } }Outputvehicle time a

Default value of bool in C#

Chandu yadav
Updated on 22-Jun-2020 13:48:03

2K+ Views

Use the default operator to get the default value of bool type −bool a = default(bool);Above, we have used the default keyword to get the default value.Let us see the code to display default value of bool −Example Live Demousing System; public class Demo {    public static void Main() {       bool a = default(bool);       // default for bool       Console.WriteLine("Default for bool type = "+a);    } }OutputThe following is the output. It shows a blank space i.e. False.Default for bool type = False

Default value of StringBuilder in C#

Arjun Thakur
Updated on 22-Jun-2020 13:48:23

428 Views

Use the default operator to get the default value of StringBuilder.StringBuilder str = default(StringBuilder);Above, we have used the default keywords to get the default value.Let us see the complete code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       StringBuilder str = default(StringBuilder);       Console.WriteLine("Default for StringBuilder = "+str);    } }OutputDefault for StringBuilder =The following is the output. It shows a blank space i.e. Null.Default for StringBuilder = Null

Default operator in C#

Ankith Reddy
Updated on 22-Jun-2020 13:48:48

519 Views

Using the default, you can get the default value of every reference and value type. The expressions set as default are evaluated at compile-time.To get the default for int −default(int);To get the default for long −default(long)Let us see the code to display default values −Example Live Demousing System; public class Demo {    public static void Main() {       int val1 = default(int);       long val2 = default(long);       bool val3 = default(bool);       // default for int       Console.WriteLine(val1);       // default for long       Console.WriteLine(val2);       // default for bol       Console.WriteLine(val3);    } }Output0 0 False

Bool Array in C#

George John
Updated on 22-Jun-2020 13:49:25

7K+ Views

In a bool array, you can store true and false values. To set a bool array, use the new operator −bool[] arr = new bool[5];To add elements in the array −arr[0] = true; arr[1] = true; arr[2] = false; arr[3] = true; arr[4] = true;Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       bool[] arr = new bool[5];       arr[0] = true;       arr[1] = true;       arr[2] = false;       arr[3] = true;       arr[4] = true;       Console.WriteLine("Displaying values...");       foreach (bool res in arr) {          Console.WriteLine(res);       }    } }OutputDisplaying values... True True False True True

C# program to separate joined strings in C#

Chandu yadav
Updated on 22-Jun-2020 13:49:53

41 Views

The following is the string array −string[] str = { "Java", "AngularJS", "Python", "jQuery", "HTML5" };Firstly, join it −string.Join(" ", str);Now to separate the above joined strings, use the Split() method as shown in the following code −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = { "Java", "AngularJS", "Python", "jQuery", "HTML5" };       // join words       string res = string.Join(" ", str);       Console.WriteLine("Joined Strings... "+res);       string[] convert = res.Split(' ');       Console.WriteLine("Separate Joined Strings..."); ... Read More

C# program to join words into a string in C#

Arjun Thakur
Updated on 22-Jun-2020 13:50:17

182 Views

Declare a string and add elements −string[] str = { "One", "Two", "Three", "Four", "Five" };Use the Join() method to join the words−string res = string.Join(" ", str);Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = { "One", "Two", "Three", "Four", "Five" };       // join words       string res = string.Join(" ", str);       Console.WriteLine(res);    } }OutputOne Two Three Four Five

C# program to find the index of a word in a string

Ankith Reddy
Updated on 22-Jun-2020 13:50:59

473 Views

Declare and initialize an array −string[] str = new string[] {    "Cat",    "Mat",    "Rat" };Now, ue IndexOf() method to find the index of the word “Mat” −Array.IndexOf(str, "Mat");The following is the code −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = new string[] {          "Cat",          "Mat",          "Rat"       };       Console.WriteLine("Our Array =");       for (int i = 0; i < str.Length; i++) {          string res = str[i];          Console.WriteLine(res);       }       int findIndex = Array.IndexOf(str, "Mat");       Console.Write("Element Mat found at the following index: ");       Console.WriteLine(findIndex);    } }OutputOur Array = Cat Mat Rat Element Mat found at the following index: 1

C# program to create an empty string array

George John
Updated on 22-Jun-2020 13:51:23

3K+ Views

To create an empty string array −string[] str = new string[] {};Above, we haven’t added elements to the array, since it is empty.Even if we will loop though the array, it won’t display anything as shown below −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = new string[] {};       Console.WriteLine("String Array elements won't get displayed since it's empty...");       for (int i = 0; i < str.Length; i++) {          string res = str[i];          Console.WriteLine(res);       }    } }OutputString Array elements won't get displayed since it's empty...

C# program to iterate over a string array with for loop

Chandu yadav
Updated on 22-Jun-2020 13:52:06

2K+ Views

Create a string array −string[] str = new string[] {    "Videos",    "Tutorials",    "Tools",    "InterviewQA" };Loop until the length of the array −for (int i = 0; i < str.Length; i++) {    string res = str[i];    Console.WriteLine(res); }Here is the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = new string[] {          "Videos",          "Tutorials",          "Tools",          "InterviewQA"       };           Console.WriteLine("String Array...");       for (int i = 0; i < str.Length; i++) {          string res = str[i];          Console.WriteLine(res);       }    } }OutputString Array... Videos Tutorials Tools InterviewQA

Advertisements