Found 34494 Articles for Programming

Print first letter of each word in a string in C#

usharani
Updated on 22-Jun-2020 12:58:16

1K+ Views

Let’s say the string is −string str = "Never Give Up!";Firstly, split each word −string[] strSplit = str.Split();Now, loop through each word and use the substring method to display the first letter as shown in the following code −Example Live Demousing System; public class Program {    public static void Main() {       string str = "Never Give Up!";       Console.WriteLine("Initial String= "+str);       Console.WriteLine("Displaying first letter of each word...");       string[] strSplit = str.Split();       foreach (string res in strSplit) {          Console.Write(res.Substring(0,1));       }    } }OutputInitial String= Never Give Up! Displaying first letter of each word... NGU

Intersect Method in C#

mkotla
Updated on 22-Jun-2020 12:58:45

371 Views

Use the Intesect method to get the common elements −Create lists −var list1 = new List{99, 87}; var list2 = new List{56, 87, 45, 99};Now, use the Intersect() method to get the common elements from the above list −list1.Intersect(list2);Here is the complete code −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {           // two lists       var list1 = new List{99, 87};       var list2 = new List{56, 87, 45, 99};       // common values       var res = list1.Intersect(list2);       foreach(int i in res) {          Console.WriteLine(i);       }    } }Output99 87

Print first letter of each word in a string using C# regex

seetha
Updated on 22-Jun-2020 12:48:42

339 Views

Let’s say our string is −string str = "The Shape of Water got an Oscar Award!";Use the following Regular Expression to display first letter of each word −@"\b[a-zA-Z]"Here is the complete code −Example Live Demousing System; using System.Text.RegularExpressions; namespace RegExApplication {    public class Program {       private static void showMatch(string text, string expr) {          Console.WriteLine("The Expression: " + expr);          MatchCollection mc = Regex.Matches(text, expr);          foreach (Match m in mc) {             Console.WriteLine(m);          }       } ... Read More

C# Equivalent to Java's Double Brace Initialization?

usharani
Updated on 22-Jun-2020 12:49:01

236 Views

Java’s Double Brace Initialization does the same work what a single brace can achieve in C#.Double Brace creates and initialize objects in a single Java expression.Let’s say the following is in Java −ExampleList list = new List() {{    add("One");    add("Two");    add("Three");    add("Four"); }}The same you can use for Collection Initializer in C# as −List list = new List() {"One","Two", “Three”, “Four”};

C# Equivalent to Java Functional Interfaces

mkotla
Updated on 22-Jun-2020 12:50:16

2K+ Views

Equivalent of Java’s Functional Interfaces in C# is Delegates.Let us see the implementation of functional interface in Java −Example@FunctionalInterface public interface MyInterface {    void invoke(); } public class Demo {    void method(){       MyInterface x = () -> MyFunc ();       x.invoke();    }    void MyFunc() {    } }The same implementation in C# delagates −Examplepublic delegate void MyInterface (); public class Demo {    internal virtual void method() {       MyInterface x = () => MyFunc ();       x();    }    internal virtual void MyFunc() {    } }

C# equivalent to Java's Thread.setDaemon?

varun
Updated on 30-Jul-2019 22:30:23

249 Views

C# equivalent to Java's Thread.setDaemon is the concept of foreground and background threads. When the foreground threads will close, the background threads will be terminated. Foreground threads continue to run until the last foreground thread is terminated. The property used for background thread is IsBackground that gets or sets a value indicating whether a thread is a background thread. The default value of this property would be false because the default threads created are Foreground Threads. To make a thread Daemon in C#, use the isBackground − Thread bgThread = new Thread(tStart); bgThread.IsBackground = true; bgThread.Start();

Global and Local Variables in C#

Giri Raju
Updated on 22-Jun-2020 12:51:26

3K+ Views

Local VariablesA local variable is used where the scope of the variable is within the method in which it is declared. They can be used only by statements that are inside that function or block of code.Example Live Demousing System; public class Program {    public static void Main() {       int a;       a = 100;       // local variable       Console.WriteLine("Value:"+a);    } }OutputValue:100Global VariablesC# do not support global variables directly and the scope resolution operator used in C++ for global variables is related to namespaces. It is called global namespace ... Read More

Generating random numbers in C#

varun
Updated on 22-Jun-2020 12:51:46

2K+ Views

To generate random numbers, use Random class.Create an object −Random r = new Random();Now, use the Next() method to get random numbers in between a range −r.Next(10,50);The following is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       Random r = new Random();       int genRand= r.Next(10,50);       Console.WriteLine("Random Number = "+genRand);    } }OutputRandom Number = 24

What is the C# equivalent to Java's isInstance()?

George John
Updated on 30-Jul-2019 22:30:23

534 Views

The java.lang.Class.isInstance() determines if the specified Object is assignment-compatible with the object represented by this Class Java’s isInstance() method’s equivalent in C# is IsAssignableFrom(). Another simplest way for isInstance() equivalent is − bool res = (ob is DemoClass); You can also work with Type.IsInstanceOfType for the same result − ob.GetType().IsInstanceOfType(otherOb)

How to convert a JavaScript array to C# array?

Ankith Reddy
Updated on 22-Jun-2020 12:52:47

1K+ Views

Let us say our JavaScript array is −    var myArr = new Array(5);    myArr[0] = "Welcome";    myArr[1] = "to";    myArr[2] = "the";    myArr[3] = "Web";    myArr[4] = "World"; Now, convert the array into a string using comma as a separator −document.getElementById('demo1').value = myArr.join(',');Now, take this to C# −string[] str = demo.Split(",".ToCharArray());The above converts the JavaScript array to C#.

Advertisements