Why does the indexing start with zero in C# arrays?

Samual Sam
Updated on 21-Jun-2020 15:26:30

759 Views

Arrays were a pointer to an address in memory of the index. This index was the 1st element of the array. Here, the index is like an offset and the concept even before C language originated.Let’s say your array elements begins from 0Xff000 and has 5 elements like {35, 23, 67, 88, 90}. Therefore, you array in memory would be like the following because int is stored using 4 bytes.0Xff000 has 35 0Xff004 has 23 0Xff008 has 67 0Xff012 has 88 0Xff016 has 90That would mean when the array is accessed, zero offsets would be index 0.Let us further see ... Read More

How to compare two tuples in C#?

Arjun Thakur
Updated on 21-Jun-2020 15:26:02

299 Views

Tuple comparison came after C# 7.3.Easily compare two tuples using the equality operator in C#.Let’s say we have two tuples −var one = (x: 1, y: 2); var two = (p: 1, 2: 3, r: 3, s:4);To compare them, just use the == operator −if (one == two) Console.WriteLine("Both the tuples are same (values are same).");Let use see the code −Examplevar one = (x: 1, y: 2); var two = (p: 1, 2: 3, r: 3, s:4); if (one == two) Console.WriteLine("Both the tuples are same (values are same)."); lse Console.WriteLine("Both the tuples values are not same.");

Reverse an array using C#

Ankith Reddy
Updated on 21-Jun-2020 15:23:20

427 Views

Firstly, set the original array −int[] arr = { 1, 2,3 }; // Original Array Console.WriteLine("Original Array= "); fo            reach (int i in arr) {    Console.WriteLine(i); }Now, use the Array.reverse() method to reverse the array −Array.Reverse(arr);The following is the complete code to reverse an array in C# −Exampleusing System; class Demo {    static void Main() {       int[] arr = { 9, 32, 87, 45, 77, 56 };       // Original Array       Console.WriteLine("Original Array= ");       foreach (int i in arr) {          Console.WriteLine(i);       }       // Reverse Array       Array.Reverse(arr);       Console.WriteLine("Reversed Array= ");       foreach (int j in arr) {          Console.WriteLine(j);       }       Console.ReadLine();    } }

What is index-based I/O BitArray collection in C#?

Chandu yadav
Updated on 21-Jun-2020 15:21:05

97 Views

The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).The following are the method of the index-based BitArray collection −Sr.No.Method & Description1public BitArray And(BitArray value);Performs the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.2public bool Get(int index);Gets the value of the bit at a specific position in the BitArray.3public BitArray Not();Inverts all the bit values in the current BitArray, so that elements set to true are changed to ... Read More

Generating password in Java

Rishi Raj
Updated on 21-Jun-2020 15:20:45

11K+ Views

Generate temporary password is now a requirement on almost every website now-a-days. In case a user forgets the password, system generates a random password adhering to password policy of the company. Following example generates a random password adhering to following conditions −It should contain at least one capital case letter.It should contain at least one lower-case letter.It should contain at least one number.Length should be 8 characters.It should contain one of the following special characters: @, $, #, !.Exampleimport java.util.Random; public class Tester{    public static void main(String[] args) {       System.out.println(generatePassword(8));    }    private ... Read More

Final local variables in C#

Samual Sam
Updated on 21-Jun-2020 15:19:47

1K+ Views

To set final for a local variable, use the read-only keyword in C#, since the implementation of the final keyword is not possible.The readonly would allow the variables to be assigned a value only once. A field marked "read-only", can only be set once during the construction of an object. It cannot be changed.Let us see an example. Below, we have set the empCount field as read-only, which once assigned cannot be changed.Exampleclass Department {    readonly int empCount;    Employee(int empCount) {       this. empCount = empCount;    }    void ChangeCount() {       //empCount = 150; // Compile error    } }

What is the default access for a class member in C#?

George John
Updated on 21-Jun-2020 15:18:21

2K+ Views

The default access for a class member in C# is private.Member variables i.e. class members are the attributes of an object (from design perspective) and they are kept private to implement encapsulation. These variables can only be accessed using the public member functions.Exampleusing System; namespace RectangleApplication {    class Rectangle {       //member variables       private double length;       private double width;       public void Acceptdetails() {          length = 10;          width = 14;       }       public ... Read More

How to copy a List collection to an array?

karthikeya Boyini
Updated on 21-Jun-2020 15:16:52

556 Views

To copy a C# list collection to an array, firstly set a list −List list1 = new List (); list1.Add("One"); list1.Add("Two"); list1.Add("Three"); list1.Add("Four");Now declare a string array and use the CopyTo() method to copy −string[] arr = new string[20]; list1.CopyTo(arr);Let us see the complete code to copy a list into a one – dimensional array.Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List list1 = new List ();       list1.Add("One");       list1.Add("Two");       list1.Add("Three");       list1.Add("Four"); ... Read More

Generating random numbers in Java

Vikyath Ram
Updated on 21-Jun-2020 15:16:08

1K+ Views

We can generate random numbers using three ways in Java.Using java.util.Random class − Object of Random class can be used to generate random numbers using nextInt(), nextDouble() etc. methods.Using java.lang.Math class − Math.random() methods returns a random double whenever invoked.Using java.util.concurrent.ThreadLocalRandom class − ThreadLocalRandom.current().nextInt() method and similar othjer methods return a random numbers whenever invoked.Exampleimport java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Tester {    public static void main(String[] args) {       generateUsingRandom();       generateUsingMathRandom();       generateUsingThreadLocalRandom();    }    private static void generateUsingRandom() {       Random random = new Random(); ... Read More

What are the differences between a list collection and an array in C#?

George John
Updated on 21-Jun-2020 15:14:18

335 Views

List collection is a generic class and can store any data types to create a list. To define a List −List l = new List();To set elements in a list, you need to use the Add method.l.Add("One"); l.Add("Two"); l.Add("Three");An array stores a fixed-size sequential collection of elements of the same type.To define Arrays −int[] arr = new int[5]; To initialize and set elements to Arrays −int[] arr = new int[5] {4, 8,5};

Advertisements