IsReadOnly Property of BitArray Class in C#

George John
Updated on 23-Jun-2020 09:57:43

140 Views

The BitArray class is used when you need to store the bits but do not know the number of bits in advance.Using the IsReadOnly class, you can get a value indicating whether the BitArray is read-only or not. ReadOnly will not allow you to add new elements to the BitArray.The following is our example stating how to use the IsReadOnly property of BitArray class in C#.Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          BitArray ba1 = new BitArray(5);          BitArray ba2 ... Read More

Keys Property of Hashtable Class in C#

Chandu yadav
Updated on 23-Jun-2020 09:56:43

147 Views

Gets an ICollection containing the keys in the Hashtable. It displays all the keys in the collection. In the below code, to get all the keys we have used a loop to loop through the collection.foreach (int k in h.Keys) {    Console.WriteLine(k); }The above displays all the keys as shown in the following code −Example Live Demousing System; using System.Collections; class Program {    static void Main() {       Hashtable h = new Hashtable();       h.Add(1, "India");       h.Add(2, "US");       h.Add(3, "UK");       h.Add(4, "Australia");       h.Add(5, "Netherland");       foreach (int k in h.Keys) {          Console.WriteLine(k);       }    } }Output5 4 3 2 1

C# Program to Add Two Matrices

Samual Sam
Updated on 23-Jun-2020 09:56:22

9K+ Views

Firstly, set three arrays.int[, ] arr1 = new int[20, 20]; int[, ] arr2 = new int[20, 20]; int[, ] arr3 = new int[20, 20];Now users will enter values in both the matrices. We have to set the row and size columns as n=3, since we want a square matrix of 3x3 size i.e 9 elements.Add both the matrices and print the third array that has the sum.for(i=0;i

C# Nested Classes

Arjun Thakur
Updated on 23-Jun-2020 09:54:58

2K+ Views

A nested class is a class declared in another enclosing class. It is a member of its enclosing class and the members of an enclosing class have no access to members of a nested class.Let us see an example code snippet of nested classes in C#.Exampleclass One {    public int num1;    public class Two {       public int num2;    } } class Demo {    static void Main() {       One a = new One();       a.num1++;       One.Two ab = new One.Two();       ab.num2++;    } ... Read More

C# Program to Generate Secure Random Numbers

karthikeya Boyini
Updated on 23-Jun-2020 09:54:27

2K+ Views

For secure random numbers, use the RNGCryptoServiceProvider Class. It implements a cryptographic Random Number Generator.Using the same class, we have found some random values using the following −using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider()) {    byte[] val = new byte[6];    crypto.GetBytes(val);    randomvalue = BitConverter.ToInt32(val, 1); }To generate random secure numbers, you can try to run the following code.Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Security.Cryptography; public class Demo {    public static void Main(string[] args) {       for (int i = 0; i

Object Creation of Inherited Class in C#

Samual Sam
Updated on 23-Jun-2020 09:53:34

2K+ Views

A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.The derived class inherits the base class member variables and member methods. Therefore, the super class object should be created before the subclass is created. You can give instructions for superclass initialization in the member initialization list.Here you can see object is created for the inherited class.Example Live Demousing System; namespace Demo {    class Rectangle {       protected double length;       protected double width;       public Rectangle(double l, ... Read More

C# Nullable DateTime

karthikeya Boyini
Updated on 23-Jun-2020 09:50:16

17K+ Views

Using the DateTime nullable type, you can assign the null literal to the DateTime type.A nullable DateTime is specified using the following question mark syntax.DateTime?The following is the code to implement Nullable Datetime.Example Live Demousing System; class Program {    static void Main() {       DateTime? dt = null;       DateFunc(dt);       dt = DateTime.Now;       DateFunc(dt);       dt = null;       Console.WriteLine(dt.GetValueOrDefault());    }    static void DateFunc(DateTime? dt) {       if (dt.HasValue) {          Console.WriteLine(dt.Value);       } else {          Console.WriteLine(0);       }    } }Output0 9/17/2018 8:27:07 AM 1/1/0001 12:00:00 AM

Date Class in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:49:22

14K+ Views

To set dates in C#, use DateTime class. The DateTime value is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D.Let’s create a DateTime object.Example Live Demousing System; class Test {    static void Main() {       DateTime dt = new DateTime(2018, 7, 24);       Console.WriteLine (dt.ToString());    } }Output7/24/2018 12:00:00 AMLet us now get the current date and time.Example Live Demousing System; class Test {    static void Main() {       Console.WriteLine (DateTime.Now.ToString());    } }Output9/17/2018 5:49:21 AMNow using the method Add(), we will add days in a date with the ... Read More

Decimal Functions in C#

Chandu yadav
Updated on 23-Jun-2020 09:44:15

290 Views

The following are some of the decimal functions in C#.Sr.No.Name & Description1Add (Decimal, Decimal)Adds two specified Decimal values.2Ceiling(Decimal)Returns the smallest integral value that is greater than or equal to the specified decimal number.3Compare (Decimal, Decimal)Compares two specified Decimal values.4CompareTo(Decimal)Compares this instance to a specified Decimal object and returns a comparison of their relative values.5CompareTo(Object)Compares this instance to a specified object and returns a comparison of their relative values.6Divide (Decimal, Decimal)Divides two specified Decimal values.7Equals(Decimal)Returns a value indicating whether this instance and a specified Decimal object represent the same value.Let us see an example of Decimal Ceiling() method in C# that returns the smallest integral value greater than or equal to ... Read More

Replace N-th Character in a String using C#

Arjun Thakur
Updated on 23-Jun-2020 09:43:40

2K+ Views

Firstly, set a string.string str1 = "Port"; Console.WriteLine("Original String: "+str1);Now convert the string into character array.char[] ch = str1.ToCharArray();Set the character you want to replace with the index of the location. To set a character at position 3rd.ch[2] = 'F';To remove nth character from a string, try the following C# code. Here, we are replacing the first character.Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       string str1 = "Port";       Console.WriteLine("Original String: "+str1);       char[] ch = str1.ToCharArray();       ch[0] = 'F';   ... Read More

Advertisements