Found 34494 Articles for Programming

C# program to find if an array contains duplicate

karthikeya Boyini
Updated on 22-Jun-2020 13:24:00

1K+ Views

Set an array −int[] arr = {    89,    12,    56,    89, };Now, create a new Dictionary −var d = new Dictionary < int, int > ();Using the dictionary method ContainsKey(), find the duplicate elements in the array −foreach(var res in arr) {    if (d.ContainsKey(res))    d[res]++;    else    d[res] = 1; }Here is the complete code −Example Live Demousing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          int[] arr = {             89, ... Read More

C# program to convert an array to an ordinary list with the same items

Samual Sam
Updated on 22-Jun-2020 13:24:36

74 Views

Set an array −int[] arr = { 23, 66, 96, 110 };Now, create a new list −var list = new List();Use the Add method and add the array elements to the list −for (int i = 0; i < arr.Length; i++) {    list.Add(arr[i]); }The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       int[] arr = { 23, 66, 96, 110 };       var list = new List();       for (int i = 0; i < arr.Length; i++) {          list.Add(arr[i]);       }       foreach(int res in list) {          Console.WriteLine(res);       }    } }Output23 66 96 110

C# program to determine if Two Words Are Anagrams of Each Other

karthikeya Boyini
Updated on 22-Jun-2020 13:25:04

6K+ Views

For anagram, another string would have the same characters present in the first string, but the order of characters can be different.Here, we are checking the following two strings −string str1 = "heater"; string str2 = "reheat";Convert both the strings into character array −char[] ch1 = str1.ToLower().ToCharArray(); char[] ch2 = str2.ToLower().ToCharArray();Now, sort them −Array.Sort(ch1); Array.Sort(ch2);After sorting, convert them to strings as shown in the following code −Example Live Demousing System; public class Demo {    public static void Main () {       string str1 = "heater";       string str2 = "reheat";       char[] ch1 ... Read More

What is the difference between virtual and abstract functions in C#?

Samual Sam
Updated on 22-Jun-2020 13:12:21

4K+ Views

Abstract methods do not provide an implementation and they force the derived classes to override the method. It is declared under abstract class. An abstract method only has the method definitionVirtual methods have an implementation, unlike the Abstract method and it can exist in the abstract and non-abstract class. It provides the derived classes with the option of overriding it.Virtual FunctionsThe virtual keyword is useful in modifying a method, property, indexer, or event. When you have a function defined in a class that you want to be implemented in an inherited class(es), you use virtual functions. The virtual functions could ... Read More

How to access array elements using a pointer in C#?

karthikeya Boyini
Updated on 03-Apr-2020 09:08:18

3K+ Views

In C#, an array name and a pointer to a data type same as the array data, are not the same variable type. For example, int *p and int[] p, are not the same type. You can increment the pointer variable p because it is not fixed in memory but an array address is fixed in memory, and you can't increment that.Here is an example −Exampleusing System; namespace UnsafeCodeApplication {    class TestPointer {       public unsafe static void Main() {          int[] list = {5, 25};          fixed(int *ptr = ... Read More

What is the difference between VAR and DYNAMIC keywords in C#?

Samual Sam
Updated on 22-Jun-2020 13:12:59

484 Views

DynamicStore any type of value in the dynamic data type variable created using dynamic keyword. Type checking for these types of variables takes place at run-time. Dynamic are dynamically typed variables.The following is the syntax for declaring a dynamic type −dynamic = value;The following is an example −dynamic val1 = 100; dynamic val2 = 5; dynamic val3 = 20;The dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at runtime.VarThe "var" keyword initializes variables with var support. Just assign ... Read More

How to compute Average for the Set of Values using C#?

karthikeya Boyini
Updated on 22-Jun-2020 13:13:36

187 Views

Firstly, set an array with values −int[] myArr = new int[] {    34,    23,    77,    67 };To get the average, firstly get the sum of array elements.Divide the sum with the length of the array and that will give you the average of the elements −int sum = 0; int average = 0; for (int i = 0; i < len; i++) {    sum += myArr[i]; } average = sum / len;The following is the complete code to get the array in C# −Example Live Demousing System; public class Program {    public static void Main() ... Read More

How to compile unsafe code in C#?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

2K+ Views

For compiling unsafe code, you have to specify the /unsafe command-line switch with command-line compiler. For example, to compile a program named one.cs containing unsafe code, from command line, give the command − csc /unsafe one.cs Under Visual Studio IDE, enable use of unsafe code in the project properties. The following are the steps − Open Project properties by double clicking the properties node in the Solution Explorer. Click on the “Build” tab. Select the option "Allow unsafe code".

File Searching using C#

Samual Sam
Updated on 22-Jun-2020 13:14:39

492 Views

To search files from the list of files in a directory, try to run the following code −Exampleusing System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          //creating a DirectoryInfo object          DirectoryInfo mydir = new DirectoryInfo(@"d:\amit");          // getting the files in the directory, their names and size          FileInfo [] f = mydir.GetFiles();          foreach (FileInfo file in f) {             Console.WriteLine("File Name: {0} Size: ... Read More

Make your collections thread-safe in C#

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

264 Views

The .NET Framework 4 brought the System.Collections.Concurrent namespace. This has several collection classes that are thread-safe and scalable. These collections are called concurrent collections because they can be accessed by multiple threads at a time. The following concurrent collection types use lightweight synchronization mechanisms: SpinLock, SpinWait, etc. These are new in .NET Framework 4. Let us see the concurrent collection in C# − Type Description BlockingCollection Bounding and blocking functionality for any type. ConcurrentDictionary Thread-safe implementation of a dictionary of key-value pairs. ConcurrentQueue Thread-safe implementation of a FIFO (first-in, first-out) queue. ConcurrentStack Thread-safe ... Read More

Advertisements