Found 34488 Articles for Programming

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

538 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#.

What is the C# equivalent of C++ friend keyword?

Arjun Thakur
Updated on 22-Jun-2020 12:53:12

2K+ Views

friend in C#A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.C++ equivalent of friend in C#The closest equivalent is to create a nested class that will access the outer class private members.Here, the inner class can ... Read More

What is the C# equivalent for the Java System.exit(0)?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

2K+ Views

The C# equivalent for Java System.exit(0) is − Environment.Exit(exitCode); The Environment.Exit() method terminates this process and returns an exit code to the operating system. Above, use exitCode as 0 (zero) to show that the process completed successfully. Use exitCode as a non-zero number to show an error, for example − Environment.Exit(1) Return a value 1 to show that the file you want is not present Environment.Exit(2)exit Return a value 2 to indicate that the file is in an incorrect format.

How to open hidden file using C#?

George John
Updated on 22-Jun-2020 12:40:11

510 Views

To open a hidden file, first make it visible. You can do this by removing the hidden attribute set on it −FileInfo file= new FileInfo(Environment.CurrentDirectory + @"\myFile.txt"); file.Attributes &= ~FileAttributes.Hidden;Now treat it as a normal text file and open it. Read the content −using (StreamReader sr = new StreamReader("myFile.txt")) {    string line;    while ((line = sr.ReadLine()) != null) {       Console.WriteLine(line);    } }After reading, set the attribute as hidden again to hide the file −file.Attributes |= FileAttributes.Hidden;

Different ways of Reading a file in C#

Chandu yadav
Updated on 22-Jun-2020 12:43:05

221 Views

Here, we are reading two different files −Reading text file −Example Live Demousing System; using System.IO; namespace FileApplication {    class Program {       static void Main(string[] args) {          try {             using (StreamReader sr = new StreamReader("d:/new.txt")) {                string line;                // Read and display lines from the file until                // the end of the file is reached.                while ((line ... Read More

How to open a plain text file in C#?

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

244 Views

To open a plain text file, use the StreamReader class. The following opens a file for reading −StreamReader sr = new StreamReader("d:/new.txt")Now, display the content of the file −while ((line = sr.ReadLine()) != null) {    Console.WriteLine(line); }Here is the code −Example Live Demousing System; using System.IO; namespace FileApplication {    class Program {       static void Main(string[] args) {          try {             using (StreamReader sr = new StreamReader("d:/new.txt")) {                string line;                // ... Read More

Advertisements