Found 34494 Articles for Programming

How to instantiate delegates in C#?

George John
Updated on 22-Jun-2020 12:17:50

962 Views

Use the new keyword to instantiate a delegate. When creating a delegate, the argument passed to the new expression is written similar to a method call, but without the arguments to the method.For example −public delegate void printString(string s); printString ps1 = new printString(WriteToScreen);You can also instantiate a delegate using an anonymous method −//declare delegate void Del(string str); Del d = delegate(string name) {    Console.WriteLine("Notification received for: {0}", name); };Let us see an example that declare and instantiates a delegate −Example Live Demousing System; delegate int NumberChanger(int n); namespace DelegateAppl {    class TestDelegate {     ... Read More

How to get last 2 characters from string in C# using Regex?

Sreemaha
Updated on 22-Jun-2020 12:16:45

905 Views

Set the string −string str = "Cookie and Session";Use the following Regex to get the last 2 characters from string −Regex.Match(str,@"(.{2})\s*$")The following is the code −Example Live Demousing System; using System.Text.RegularExpressions; public class Demo {    public static void Main() {       string str = "Cookie and Session";       Console.WriteLine(Regex.Match(str,@"(.{2})\s*$"));    } }Outputon

How to insert an item in a list at a given position in C#?

Chandu yadav
Updated on 22-Jun-2020 12:19:34

2K+ Views

To insert an item in an already created List, use the Insert() method.Firstly, set elements −List list = new List(); list.Add(989); list.Add(345); list.Add(654); list.Add(876); list.Add(234); list.Add(909);Now, let’s say you need to insert an item at 4th position. For that, use the Insert() method −// inserting element at 4th position list.Insert(3, 567);Let us see the complete example −Exampleusing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          List < int > list = new List < int > ();         ... Read More

How to get last 4 characters from string inC#?

radhakrishna
Updated on 22-Jun-2020 12:19:58

2K+ Views

Firstly, set the string −string str = "Football and Tennis";Now, use the substring() method to get the last 4 characters −str.Substring(str.Length - 4);Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       string str = "Football and Tennis";       string res = str.Substring(str.Length - 4);       Console.WriteLine(res);    } }Outputnnis

How to initialize a tuple to an empty tuple in C#?

Arjun Thakur
Updated on 22-Jun-2020 12:20:26

2K+ Views

To initialize a tuple to an empty tuple −Tuple myTuple;If you want to check for values in a tuple, that whether it is null or not −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          Tuple tuple = new Tuple(10, null);          if (tuple.Item1 == 10) {             Console.WriteLine(tuple.Item1);          }          if (tuple.Item2 == null) {             Console.WriteLine("Item is null");          }       }    } }

How to initialize a string to an empty string in C#?

Ankith Reddy
Updated on 22-Jun-2020 12:21:13

3K+ Views

To initialize a string to an empty list −string myStr = null;Now, use the built-in method IsNullOrEmpty() to check whether the list is empty or not −if (string.IsNullOrEmpty(myStr)) {    Console.WriteLine("String is empty or null!"); }Let us see the complete code −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          string myStr = null;          if (string.IsNullOrEmpty(myStr)) {             Console.WriteLine("String is empty or null!");          }          Console.ReadKey();       ... Read More

How to insert an item into a C# list by using an index?

George John
Updated on 22-Jun-2020 12:22:30

2K+ Views

Firstly, set a list −List list = new List(); list.Add(456); list.Add(321); list.Add(123); list.Add(877); list.Add(588); list.Add(459);Now, to add an item at index 5, let say; for that, use the Insert() method −list.Insert(5, 999);Let us see the complete example −Exampleusing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          List list = new List();          list.Add(456);          list.Add(321);          list.Add(123);          list.Add(877);          list.Add(588);          list.Add(459);     ... Read More

How to initialize a rectangular array in C#?

Chandu yadav
Updated on 22-Jun-2020 12:23:31

261 Views

An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.Multi-dimensional arrays are also called rectangular array. Multidimensional arrays are initialized by specifying bracketed values for each row.The following array is with 2 rows and each row has 2 columns.int [, ] a = new int [2, 2] { {20, 50} , /* initializers for row indexed by 0 */ {15, 45} , /* initializers for row indexed by 1 */ };Let us see an example ... Read More

How to obtain Status of the Current Thread in C#?

karthikeya Boyini
Updated on 22-Jun-2020 12:23:59

165 Views

To get the status of current thread, use the IsAlive() method −Firstly, create a new thread −Thread t = Thread.CurrentThread; t.Name = "Our Thread";Now, to get the status of the current thread −t.IsAliveThe following is the complete code −Example Live Demousing System; using System.Threading; namespace Demo {    class MyClass {       static void Main(string[] args) {          Thread t = Thread.CurrentThread;          t.Name = "Our Thread";          Console.WriteLine("Status = {0}", t.IsAlive);          Console.ReadKey();       }    } }OutputStatus = True

Recommended IDEs for C# on Windows/Linux/Mac OS

Samual Sam
Updated on 30-Jul-2019 22:30:23

259 Views

The best IDE for C# on Windows is Microsoft Visual Studio. It is an IDE to develop websites, web apps, mobile apps, etc. The following are the features of Visual Studio IDE − Code Editor − Visual Studio has a code editor supporting syntax highlighting and code completion using IntelliSense. Breakpoints − Set breakpoints and allow monitoring the variable values as the execution progress. Extend Capability − With Visual Studio, you can extend the functionality of the IDE. The extension includes macros, packages, etc. Built-in-languages − Visual Studio supports more than 30 programming ... Read More

Advertisements