It lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. It should be terminated with #endregion.Let us see how to define a region using #region.#region NewClass definition public class NewClass { static void Main() { } } #endregionThe following is an example showing the usage of #region directive.Example Live Demousing System; #region class MyClass { } #endregion class Demo { #region VARIABLE int a; #endregion static void Main() { #region BODY Console.WriteLine("Example showing the usage of region directive!"); #endregion } }OutputExample showing the usage of region directive!
Use operator + to concatenate two or more string objects.Set the first string object.char[] c1 = { 'H', 'e', 'n', 'r', 'y' }; string str1 = new string(c1);Now, set the second string object.char[] c2 = { 'J', 'a', 'c', 'k' }; string str2 = new string(c2);Now display the concatenated strings using + operator.Example Live Demousing System.Text; using System; class Program { static void Main() { char[] c1 = { 'H', 'e', 'n', 'r', 'y' }; string str1 = new string(c1); char[] c2 = { 'J', 'a', 'c', 'k' }; string str2 = new string(c2); Console.WriteLine("Welcome " + str1 + " and " + str2 + "!"); } }OutputWelcome Henry and Jack!
Set a list.List myList = new List(){1, 2, 3, 5, 8, 9};Now, get the first and last elements −int a = myList.OrderBy(x => x).First(); int b = myList.OrderBy(x => x).Last();In a new list, get all the elements and use the Except to get the missing numbers −List myList2 = Enumerable.Range(a, b - a + 1).ToList(); List remaining = myList2.Except(myList).ToList();Let us see the complete code −Example Live Demousing System.Collections.Generic; using System; using System.Linq; public class Program { public static void Main() { List myList = new List(){1, 2, 3, 5, 8, 9}; Console.WriteLine("Numbers... "); ... Read More
Use the CSS :last-child selector to style every elements that is the last child of its parent. You can try to run the following code to implement the :last-child selectorLive Demo p:last-child { background: orange; } This is demo text1. This is demo text2. This is demo text3.
If a directory you are trying to find does not exist, then DirectoryNotFoundException occurs.Here, we are try to find a directory that does not exist using GetDirectories() method.Exampleusing System.IO; using System; class Program { static void Main() { Directory.GetDirectories("D:ew\"); } }The above code will generate the following exception since the directory “D:ew” does not exist.Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path
If a File you are trying to find does not exist, then FileNotFoundException occurs.Here, we are try to find a file that does not exist using StreamReader() method.reader = new StreamReader("new.txt"))To read it we have used the following method −reader.ReadToEnd();Let us see the complete code.Exampleusing System.IO; using System; class Program { static void Main() { using (StreamReader reader = new StreamReader("new.txt")) { reader.ReadToEnd(); } } }The above code will generate the following exception since the file “new.txt” does not exist.Unhandled Exception: System.IO.FileNotFoundException: Could not find file …
Let’s say you have a ValueTuple and would like to convert it to a tuple, then use the ToTuple() method.With C#, we can easily convert a ValueTuple to a Tuple using ToTuple() method.Note − Add System.ValueTuple package to run ValueTuple program.Let’s see how to add it −Go to your projectRight click on the project in the solution explorerSelect “Manage NuGet Packages”You will reach the NuGet Package Manager.Now, click the Browse tab and find “ValueTuple”Finally, add System.ValueTuple packageLet us see an example to implement ToTuple() method.Exampleusing System; class Program { static void Main() { var val = ... Read More
Firstly, set a list and add elements.List myList = new List(); myList.Add("Jennings"); myList.Add("James"); myList.Add("Chris");Let’s say you need to delete the element “James” now. For that, use the Remove() method.myList.Remove("James");Here is the complete code.Example Live Demousing System.Collections.Generic; using System; class Program { static void Main() { List myList = new List(); myList.Add("Jennings"); myList.Add("James"); myList.Add("Chris"); Console.WriteLine("Initial List..."); foreach(string str in myList) { Console.WriteLine(str); } myList.Remove("James"); Console.WriteLine("New List..."); ... Read More
Set an array.int[] array = new int[] { 50, 100, 150, 200, 250, 300, 350, 400 };Now, if you will print this array using a loop and new line, then the arrays will be visible vertically.To work it horizontally, use the Join() method and set spaces to separate array elements.string.Join(" ", array)Let us see the complete code.Example Live Demousing System; using System.Linq; using System.IO; class Program { static void Main() { int[] array = new int[] { 50, 100, 150, 200, 250, 300, 350, 400 }; Console.WriteLine(string.Join(" ", array)); } }Output50 100 150 200 250 300 350 400
To handle File Paths in C#, use the Path methods. These methods come under System.IO Namespace.Some of them are −GetExtensionRetrieve the extension of the file using the GetExtension() method.For example, .txt, .dat, etc.GetFileNameRetrieve the name of the file using the GetFileName() method.For example, new.txt, details.dat, etc.GetFileNameWithoutExtensionRetrieve the name of the file without extension using the GetFileNameWithoutExtension() method.For example, new, details, etc.Let us see an example −Example Live Demousing System.IO; using System; class Program { static void Main() { string myPath = "D:\one.txt"; string fileExtension = Path.GetExtension(myPath); string fileName = Path.GetFileName(myPath); ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP