Csharp Articles

Page 92 of 196

Random Numbers in C#

karthikeya Boyini
karthikeya Boyini
Updated on 22-Jun-2020 22K+ Views

To generate random numbers in C#, use the Next(minValue, MaxValue) method. The parameters are used to set the minimum and maximum values.Next(100,200);We have set the above method under Random() object.Random rd = new Random(); int rand_num = rd.Next(100,200);The following is an example −Example Live Demousing System; class Program {    static void Main() {       Random rd = new Random();       int rand_num = rd.Next(100,200);       Console.WriteLine(rand_num);    } }Output182

Read More

Remove Leading Zeros from a String in C#

Ankith Reddy
Ankith Reddy
Updated on 22-Jun-2020 14K+ Views

Let’s say the following is our string with leading zeros.String str ="000234";Use the TrimStart() method and set the 0 to remove it.TrimStart(new Char[] { '0' } )The following is the complete code to remove leading zeros.Example Live Demousing System; class Program {    static void Main() {       String str ="000234".TrimStart(new Char[] { '0' } );       Console.WriteLine(str);    } }Output234

Read More

Typeof() vs GetType() in C#

Samual Sam
Samual Sam
Updated on 22-Jun-2020 17K+ Views

Typeof()The type takes the Type and returns the Type of the argument.For example: System.Byte for the following −typeof(byte)The following is an example −Example Live Demousing System; class Program {    static void Main() {       Console.WriteLine(typeof(int));       Console.WriteLine(typeof(byte));    } }OutputSystem.Int32 System.ByteGetType()The GetType() method of array class in C# gets the Type of the current instance.To get the type.Type tp = value.GetType();In the below example, we are checking the int value using the type.if (tp.Equals(typeof(int))) Console.WriteLine("{0} is an integer data type.", value)The following is the usage of GetType() method in C#.Example Live Demousing System; class Program { ...

Read More

Using the new keyword in C#

Arjun Thakur
Arjun Thakur
Updated on 22-Jun-2020 8K+ Views

Use the new keyword to create an instance of the array. The new operator is used to create an object or instantiate an object. Here in the example an object is created for the class using the new.The following is an example.Calculate c = new Calculate();You can also use the new keyword to create an instance of the array.double[] points = new double[10];The new keyword is also used to create object of a collection.SortedList sl = new SortedList(); // SortedList List myList = new List() // ListLet us see an example.Example Live Demousing System; class Program {    static void Main() ...

Read More

How to generate the first 100 even Numbers using C#?

karthikeya Boyini
karthikeya Boyini
Updated on 22-Jun-2020 3K+ Views

To generate first 100 even numbers, set a for loop from 1 to 100.for(val = 1; val

Read More

Write a C# program to find GCD and LCM?

Ankith Reddy
Ankith Reddy
Updated on 22-Jun-2020 1K+ Views

GCD (Greatest Common Divisor)GCD is the largest positive integer that divides each of the integers.LCM (Least Common Multiple)LCM of two numbers is the smallest integer divisible by both the numbers.The following is an example to calculate the GCD and LCM. Here, we are calculating the LCM and GCD of 10 and 16 −Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class Program {       static void Main(string[] args) {          int val1, val2, n1, n2, x;          int resLCM, resGCD;          val1 ...

Read More

What is the difference between String.Copy() and String.CopyTo() methods in C#?

Arjun Thakur
Arjun Thakur
Updated on 22-Jun-2020 387 Views

String.CopyTo() method gets the string characters and places them into an array. A group of characters are copied from source string into a character array.The following is the Copy() method −Example Live Demousing System; class Demo {    static void Main(String[] args) {       string str = "This is it!";       char[] ch = new char[5];       str.CopyTo(2, ch, 0, 2);       Console.WriteLine("Output...");       Console.WriteLine(ch);    } }OutputOutput... isString.Copy() creates a new string object with similar content.Example Live Demousing System; class Demo {    static void Main(String[] args) { ...

Read More

What is method hiding in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 22-Jun-2020 2K+ Views

Method hiding is also known as shadowing. The method of the parent class is available to the child class without using the override keyword in shadowing. The child class has its own version of the same function.Use the new keyword to perform shadowing.Let us see an example.Example Live Demousing System; using System.Collections.Generic; class Demo {    public class Parent {       public string GetInfo () {          return "This is Parent Class!";       }    }    public class Child : Parent {       public new string GetInfo() {   ...

Read More

What is static binding in C#?

Samual Sam
Samual Sam
Updated on 22-Jun-2020 1K+ Views

The linking of a function with an object during compile time is called static binding. C# provides two techniques to implement static polymorphism: Function overloading and Operator overloading.In Function Overloading, you can have multiple definitions for the same function name in the same scope.Examplevoid print(int i) {    Console.WriteLine("Printing int: {0}", i ); } void print(double f) {    Console.WriteLine("Printing float: {0}" , f); }Overloaded operators are functions with special names. The keyword operator IS followed by the symbol for the operator being defineD.Examplepublic static Box operator+ (Box b, Box c) {    Box box = new Box();   ...

Read More

What is the difference between a mutable and immutable string in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 22-Jun-2020 4K+ Views

Mutable stringStringBuilder is a mutable string in C#. With StringBuilder, you can expand the number of characters in the string. The string cannot be changed once it is created, but StringBuilder can be expanded. It does not create a new object in the memory.Set StringBuilder −StringBuilder str = new StringBuilder();Let us see an example to learn how to work with StringBuilder in C# −Example Live Demousing System; using System.Text; public class Program {    public static void Main() {       StringBuilder str = new StringBuilder("Web World!!", 30);       str.Replace("World", "Arena");       Console.WriteLine(str);   ...

Read More
Showing 911–920 of 1,951 articles
« Prev 1 90 91 92 93 94 196 Next »
Advertisements