Found 2628 Articles for Csharp

TrimEnd() method in C#

George John
Updated on 22-Jun-2020 07:37:29

5K+ Views

The TrimEnd() method removes all trailing occurrences of a set of characters specified in an array.For example, the following string has trailing 1s.String str ="234561111".We can easily remove the above trailing 1s from a string using the TrimEnd() method.Let us see an example to remove all trailing 1s.Example Live Demousing System; class Program {    static void Main() {       String str ="234561111".TrimEnd(new Char[] { '1' } );       Console.WriteLine(str);    } }Output23456

The Object Class in C#

Samual Sam
Updated on 22-Jun-2020 07:38:24

1K+ Views

The Object class is the base class of all the classes in C#. It has the following methods on C#.Sr.NoMethod & Description1Equals(Object)Determines whether the specified object is equal to the current object.2Equals(Object, Object, Determines whether the specified object instances are considered equal.3Finalize()Allows an object to try to free resources4GetHashCode()default hash function.5GetType()Type of the current instance.6MemberwiseClone()shallow copy of the current Object.7ReferenceEquals(Object, Object)Determines whether the specified Object instances are the same instance.8ToString()Returns a string that represents the current object.Let us see an example how to create an object of a class in C#.Example Live Demousing System; namespace MyApplication {    class Demo { ... Read More

Different ways for Integer to String Conversions in C#

Chandu yadav
Updated on 22-Jun-2020 07:39:03

12K+ Views

The easiest way to convert an integer to a string in C# is using the ToString() method.Let us see an example −int a = 100; string str = a.ToString();Another way is to use Convert.ToString();b = 50; string str2 = Convert.ToString(b); Console.WriteLine(str2);The following is the example showing the different ways to convert integer to string.Example Live Demousing System; class Program {    static void Main() {       int a, b, c;       a = 10;       string str = a.ToString();       Console.WriteLine(str);       b = 50;       string str2 = Convert.ToString(b);       Console.WriteLine(str2);       c = 100;       string str3 = string.Format("{0}", c);       Console.WriteLine(str3);    } }Output10 50 100

Using the new keyword in C#

Arjun Thakur
Updated on 22-Jun-2020 07:25:22

7K+ 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
Updated on 22-Jun-2020 07:24:40

2K+ Views

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

Typeof() vs GetType() in C#

Samual Sam
Updated on 22-Jun-2020 07:26:04

15K+ 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

Remove Leading Zeros from a String in C#

Ankith Reddy
Updated on 22-Jun-2020 07:26:30

10K+ 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

Random Numbers in C#

karthikeya Boyini
Updated on 22-Jun-2020 07:27:06

18K+ 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

What is the difference between Write() and WriteLine() methods in C#?

Samual Sam
Updated on 22-Jun-2020 07:28:54

5K+ Views

The difference between Write() and WriteLine() method is based on new line character.Write() method displays the output but do not provide a new line character.WriteLine() method displays the output and also provides a new line character it the end of the string, This would set a new line for the next output.Let us see an example to learn about the difference between Write() and WriteLine() method −Example Live Demousing System; class Program {    static void Main() {       Console.Write("One");       Console.Write("Two");       // this will set a new line for the next output ... Read More

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

Chandu yadav
Updated on 22-Jun-2020 07:31:12

535 Views

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

Advertisements