Found 2628 Articles for Csharp

What does Array.SyncRoot property of array class do in C#?

Arjun Thakur
Updated on 20-Jun-2020 16:31:20

255 Views

The Array.SyncRoot property is used to get an object that can be used to synchronize access to the Array. The classes that have arrays can also use the SyncRoot property to implement their own synchronization.Enumerating through a collection is not a thread safe procedure. The other threads may modify the collection even when the collection is synchronized. This would eventually cause the enumerator to throw an exception. For this, you need to lock the collection.Let us see an example to work with Array.SyncRoot property −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {   ... Read More

What are the C++ features missing in C#?

Chandu yadav
Updated on 20-Jun-2020 16:12:29

185 Views

C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg.C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.The following are some of the features of C++ missing in C# −In C#, Multiple Inheritance is not possible, whereas C++ can easily implement Multiple Inheritance.In C++, you need to manage memory manually and must allocate and deallocate memory for your objects.C++ can create standalone applications, whereas C# ... Read More

HashSet in C#

George John
Updated on 20-Jun-2020 16:13:36

590 Views

HashSet in C# eliminates duplicate strings or elements in an array.In C#, it is an optimized set collection.Let us see an example to remove duplicate strings using C# HashSet. Here, we have duplicate elements −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {          "bus",          "truck",          "bus",          "car",          "truck"       };       Console.WriteLine(string.Join(", ", arr1));       // HashSet ... Read More

Integer literals vs Floating point literals in C#

Chandu yadav
Updated on 20-Jun-2020 16:16:35

299 Views

Integer LiteralsAn integer literal can be a decimal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal. Here are some of the examples of integer literals −10 // int 18u // unsigned intLet’s use the above literal while declaring and initializing a variable −// int int a =10;We will now print the values −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          // int          int a =200;   ... Read More

How to use ReadKey() method of Console class in C#?

George John
Updated on 20-Jun-2020 16:19:06

1K+ Views

Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.A common use of the ReadKey() method is that you can halt the program execution. This could be done until a key is pressed by the user.Let us see an example −Example Live Demousing System; public class Demo {    public static void Main() {       DateTime date = DateTime.Now;       TimeZoneInfo timeZone = TimeZoneInfo.Local;       Console.WriteLine("Time Zone = {0}", timeZone.IsDaylightSavingTime(date) ... Read More

How to determine if the string has all unique characters using C#?

Arjun Thakur
Updated on 20-Jun-2020 16:21:24

282 Views

To determine if a string has unique characters or not, firstly check a word in the string with the next word −for (int j = i + 1; j < val.Length; j++) {    if (val[i] == val[j]) }If you find a match, that would mean the string do not have unique characters.If you are unable to find a match, then the string has all unique characters.In case of a match, return false i.e. unique characters not found −for (int j = i + 1; j < val.Length; j++) {    if (val[i] == val[j])    return false; }

What does Array.IsSynchronized property of array class do in C#?

Arjun Thakur
Updated on 20-Jun-2020 16:22:10

66 Views

The Array.IsSynchronized property in C gets a value indicating whether access to the Array is synchronized.The IsSynchronized property is implemented by Arrays because it is needed by the System.Collections.ICollection interface. Classes using arrays can also implement own synchronization using the SyncRoot property.The following is the syntax −public bool IsSynchronized { get; }The Array.IsSynchronized property implementation is the same like the SyncRoot property −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       Array arr = new int[] { 2, 1, 9, 4, 8, 6,8 };       lock(arr.SyncRoot) {          foreach (Object val in arr)          Console.WriteLine(val);       }    } }Output2 1 9 4 8 6 8

What are the different ways for a method to be overloaded in C#?

Ankith Reddy
Updated on 20-Jun-2020 16:04:34

160 Views

The different ways with which a method can be overloaded is −The datatypes of parameters are different The number of parameters are differentThe following gives an example stating different datatypes of parameters −void print(int i) {    Console.WriteLine("Printing int: {0}", i ); } void print(double f) {    Console.WriteLine("Printing float: {0}" , f); } void print(string s) {    Console.WriteLine("Printing string: {0}", s); }The following states the different number of parameters −// two parameters public static int mulDisplay(int one, int two) {    return one * two; } // three parameters public static int mulDisplay(int one, int ... Read More

How to control for loop using break and continue statements in C#?

George John
Updated on 20-Jun-2020 16:05:10

133 Views

Break statement terminates the loop. To use it in a for loop, you can get input from user everytime and display the output when user enters a negative number. The output gets displayed then and exited using the break statement −for(i=1; i

How to delete/remove an element from a C# array?

Chandu yadav
Updated on 20-Jun-2020 16:06:09

9K+ Views

To delete an elements from a C# array, we will shift the elements from the position the user want the element to delete.Here, first we have 5 elements −int[] arr = new int[5] {35, 50, 55, 77, 98};Now let’s say we need to delete the element at 2nd position i.e. variable “pos = 2” is set, for that shift the elements after the specified position −// Shifting elements for (i = pos-1; i < 4; i++) {    arr[i] = arr[i + 1]; }Now display the result as shown in the complete code below.Example Live Demousing System; using System.Collections.Generic; using System.Linq; ... Read More

Advertisements