Csharp Articles - Page 135 of 258

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

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

What is the Keys property of Hashtable class in C#?

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

142 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

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

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

128 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

C# program to accept two integers and return the remainder

karthikeya Boyini
Updated on 23-Jun-2020 09:58:59

704 Views

Firstly, set the two numbers.int one = 250; int two = 200;Now pass those numbers to the following function.public int RemainderFunc(int val1, int val2) {    if (val2 == 0)    throw new Exception("Second number cannot be zero! Cannot divide by zero!");    if (val1 < val2)    throw new Exception("Number cannot be less than the divisor!");    else    return (val1 % val2); }Above we have checked for two conditions i.e.If the second number is zero, an exception occurs.If the first number is less than the second number, an exception occurs.To return remainder of two numbers, the following is ... Read More

C# program to remove n-th character from a string

Samual Sam
Updated on 23-Jun-2020 09:43:17

879 Views

To remove a character, use the remove() method and set the index from where you want to delete the character.Firstly, set the string.string str1 = "Amit"; Console.WriteLine("Original String: "+str1);To delete a character at position 4.StringBuilder strBuilder = new StringBuilder(str1); strBuilder.Remove(3, 1);You can try to run the following code to remove nth character from a string.Example Live Demousing System; using System.Text; public class Demo {    public static void Main(string[] args) {       string str1 = "Amit";       Console.WriteLine("Original String: "+str1);       StringBuilder strBuilder = new StringBuilder(str1);       strBuilder.Remove(3, 1);       str1 ... Read More

Covariance and Contravariance in C#

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

333 Views

To deal with classes effectively, use the concept of covariance and contra variance. Let us consider the following as our class. One is a base class for class Two, whereas Two is a base class for Three. class One { } class Two: One { } class Three : Two { } A base class can hold a derived class, but the opposite is not possible. With Covariance, you can pass a derived type where a base type is expected. Co-variance can be used on array, interface, delegates, etc in C#. Contra variance is for parameters. ... Read More

C# program to replace n-th character from a given index in a string

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

Cohesion in C#

Samual Sam
Updated on 30-Jul-2019 22:30:23

2K+ Views

Cohesion in C# shows the relationship within modules. It shows the functional strength of the modules. The greater the cohesion, the better will be the program design. It is the dependency between the modules internal elements like methods and internal modules. High cohesion will allow you to reuse classes and method. An example of High cohesion can be seen in System.Math class i.e.it has Mathematical constants and static methods − Math.Abs Math.PI Math.Pow A class that does a lot of things at a time is hard to understand and maintain. This is what we call low cohesion and ... Read More

Advertisements