Found 2587 Articles for Csharp

How to create a thread in C#?

Chandu yadav
Updated on 23-Jun-2020 12:11:59

688 Views

Threads are lightweight processes. A thread is defined as the execution path of a program. Threads are created by extending the Thread class. The extended Thread class then calls the Start() method to begin the child thread execution.Example of Thread: One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.The following is an example showing how to create a thread.Example Live Demousing System; using System.Threading; namespace Demo {    class Program {       public static void ThreadFunc() {   ... Read More

How to create a tuple with string and int items in C#?

karthikeya Boyini
Updated on 23-Jun-2020 12:11:10

412 Views

Firstly, set two items in the tuple.Tuple tuple = new Tuple(20, "Tom");Now check for first item in the tuple, which is an integer.if (tuple.Item1 == 20) {    Console.WriteLine(tuple.Item1); }Now check for second item in the tuple, which is a string −if (tuple.Item2 == "Tom") {    Console.WriteLine(tuple.Item2); }The following is an example to create a tuple with string and int items.Example Live Demousing System; using System.Threading; namespace Demo {    class Program {       static void Main(string[] args) {          Tuple tuple = new Tuple(20, "Tom");          if (tuple.Item1 == 20) { ... Read More

How to find maximum between 2 numbers using C#?

George John
Updated on 23-Jun-2020 12:12:53

1K+ Views

Firstly, declare and initialize two numbers.int num1 = 50; int num2 = 90;With that, use if-else to find the maximum number.if (num1 > num2) {    maxNum = num1; } else {    maxNum = num2; }Above, we have set the maximum value to the variable maxNum and printed it later on.The following is the complete example to find maximum between 2 numbers in C#.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          int num1 = 50;          int num2 = 90;     ... Read More

How to initialize an array in C#?

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

2K+ Views

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Firstly, declare an array. int[] rank; But declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array. Array is a reference type, so you need to use the new keyword to create an instance of the array. For example, int[] rank = new int[5]; You can assign values to an array at the time of declaration. int[] rank ... Read More

How to Initialize and Compare Strings in C#?

karthikeya Boyini
Updated on 23-Jun-2020 11:45:50

243 Views

To initializes a string in C# is an easy task. Let’s say you want to set a name “Amit”, for that, initialize your string as.String str1 = "Hello, World!";To compare strings, use the the following C# method.public static int Compare(string str1, string str2)To compare, if −String.Compare(str1, str2) == 0If the above is equal to 0, then both the strings are equal.The above method compares two specified string objects and returns an integer that indicates their relative position in the sort order.The following is an example that shows the comparison of one string to another.Example Live Demousing System; namespace Demo {   ... Read More

What is the Length property of BitArray class in C#?

Samual Sam
Updated on 23-Jun-2020 11:47:02

98 Views

The length property is used to gets or sets the number of elements in the BitArray.Our BitArray.BitArray arr = new BitArray( 5 );To calculate the length, use the length property.Console.WriteLine( "Length: {0}", arr.Length );You can try to run the following code to learn how to work with Length property of BitArray class.Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() {       BitArray arr = new BitArray( 5 );       Console.WriteLine( "Count: {0}", arr.Count );       Console.WriteLine( "Length: {0}", arr.Length );    } }OutputCount: 5 Length: 5

What is the IsReadOnly property of ArrayList class in C#?

karthikeya Boyini
Updated on 23-Jun-2020 11:48:23

155 Views

The IsReadOnly property of ArrayList class is useful to get a value indicating whether the ArrayList is read-only.Firstly, we have the following ArrayList.ArrayList arrList = new ArrayList();Then we have checked using the IsReadOnly Property.Console.WriteLine("myArrayList.IsReadOnly = " + arrList.IsReadOnly);The following is an example showing how to work with IsReadOnly property in ArrayList class.Example Live Demousing System; using System.Collections; class Demo {    public static void Main() {       ArrayList arrList = new ArrayList();       Console.WriteLine("myArrayList.IsReadOnly = " + arrList.IsReadOnly);    } }OutputmyArrayList.IsReadOnly = False

C# program to check whether a list is empty or not

Chandu yadav
Updated on 23-Jun-2020 11:48:02

10K+ Views

Use lists in C# to store elements and fetch it. Let us see an example.Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var subjects = new List();       subjects.Add("Maths");       subjects.Add("Java");       subjects.Add("English");       subjects.Add("Science");       subjects.Add("Physics");       subjects.Add("Chemistry");       foreach (var sub in subjects) {          Console.WriteLine(sub);       }    } }OutputMaths Java English Science Physics ChemistryNow to check whether a list is empty or not, use the Count ... Read More

What is the IsFixedSize property of ArrayList class in C#?

Samual Sam
Updated on 23-Jun-2020 11:48:55

124 Views

The IsFixedSize property of ArrayList class is used to get a value indicating whether the ArrayList has a fixed size.The following is an example stating the usage of isFixedSize property.Example Live Demousing System; using System.Collections; class Demo {    public static void Main() {       ArrayList arrList = new ArrayList();       Console.WriteLine("Adding some numbers:");       arrList.Add(45);       arrList.Add(78);       Console.WriteLine(arrList.Count);       Console.WriteLine("myArrayList.IsFixedSize = " + arrList.IsFixedSize);    } }OutputAdding some numbers: 2 myArrayList.IsFixedSize = FalseAbove we have added an array list.ArrayList arrList = new ArrayList();Then we have checked whether ... Read More

What is the scope resolution operator in C#?

karthikeya Boyini
Updated on 23-Jun-2020 11:50:48

1K+ Views

The scope resolution operator in C# has a different meaning as compared with C++. In C++ the :: is used for global variables, whereas in C# it is related to namespaces.If you have a type that share an identifier in different namespace, then to identify them use the scope resolution operator.For example, to reference System.Console class, use the global namespace alias with the scope resolution operator.global::System.ConsoleExample Live Demousing myAlias = System.Collections; namespace Program {    class Demo {       static void Main() {          myAlias::Hashtable h = new myAlias::Hashtable();          h.Add("Q", "1");   ... Read More

Advertisements