Csharp Articles

Page 113 of 196

Random Numbers in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 22K+ Views

The Random class in C# is used to generate pseudo-random numbers. It provides various methods to generate random integers, doubles, and bytes within specified ranges. Syntax Following is the syntax for creating a Random object and generating random numbers − Random rd = new Random(); int randomNumber = rd.Next(minValue, maxValue); Following are the commonly used methods of the Random class − rd.Next() // Random int from 0 to int.MaxValue rd.Next(maxValue) ...

Read More

Remove Leading Zeros from a String in C#

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 14K+ Views

Removing leading zeros from a string is a common requirement in C# when dealing with numeric data or formatted strings. There are several approaches to accomplish this task, with TrimStart() being the most straightforward method. Syntax Following is the syntax for using TrimStart() to remove leading zeros − string.TrimStart('0') string.TrimStart(new char[] { '0' }) Using TrimStart() Method The TrimStart() method removes specified characters from the beginning of a string. To remove leading zeros, we pass '0' as the character to trim − using System; class Program { ...

Read More

Typeof() vs GetType() in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 17K+ Views

In C#, both typeof() and GetType() are used to obtain type information, but they work differently. typeof() is a compile-time operator that returns the Type object for a specified type, while GetType() is a runtime method that returns the actual type of an object instance. Syntax Following is the syntax for using typeof() operator − Type type = typeof(TypeName); Following is the syntax for using GetType() method − Type type = objectInstance.GetType(); Using typeof() Operator The typeof() operator takes a type name as its argument and returns the Type ...

Read More

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

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 3K+ Views

To generate the first 100 even numbers in C#, you can use a for loop and check if each number is divisible by 2. An even number has no remainder when divided by 2, which can be tested using the modulo operator (%). What are Even Numbers? Even numbers are integers that are divisible by 2 without a remainder. Examples include 2, 4, 6, 8, 10, and so on. The condition number % 2 == 0 identifies even numbers. Even Number Test Number 8 ...

Read More

Using the new keyword in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 8K+ Views

The new keyword in C# is used to create instances of objects, arrays, and collections. It allocates memory for the object and calls the appropriate constructor to initialize it. Syntax Following is the syntax for using the new keyword to create objects − ClassName objectName = new ClassName(); Following is the syntax for creating arrays using new − dataType[] arrayName = new dataType[size]; Following is the syntax for creating collections using new − CollectionType collectionName = new CollectionType(); Using 'new' to Create Objects The most ...

Read More

Different ways for Integer to String Conversions in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 16K+ Views

Converting integers to strings is a common operation in C# programming. There are several built-in methods available, each with different use cases and performance characteristics. Syntax Following are the main syntaxes for integer to string conversion − // Using ToString() method string str = number.ToString(); // Using Convert.ToString() method string str = Convert.ToString(number); // Using string interpolation string str = $"{number}"; // Using string.Format() method string str = string.Format("{0}", number); Using ToString() Method The ToString() method is the most commonly used approach for converting integers to strings. It's called directly ...

Read More

The Object Class in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

The Object class is the base class of all classes in C# and the .NET Framework. Every class in C# implicitly inherits from the Object class, which means all objects have access to the fundamental methods provided by this base class. When you create any class in C#, it automatically inherits from Object even if you don't explicitly specify it. This provides a common set of methods that every object can use. Syntax Every class implicitly inherits from Object − public class MyClass { // This class inherits from Object automatically } ...

Read More

What is the difference between declaration and definition in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 12K+ Views

In C#, declaration means specifying the type and name of a variable, method, or class without providing an initial value or implementation. Definition (or initialization) means providing an actual value or implementation to what was declared. This distinction applies to variables, arrays, methods, and classes. Understanding the difference helps write cleaner code and avoid compilation errors. Syntax Following is the syntax for declaration − datatype variableName; Following is the syntax for definition/initialization − variableName = value; // or combined declaration and initialization datatype variableName = value; Variable Declaration vs ...

Read More

What is the difference between dynamic type variables and object type variables?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

In C#, both dynamic and object type variables can store values of any type, but they differ significantly in how type checking is performed. Understanding this difference is crucial for writing efficient and safe C# code. The object type is the ultimate base class for all data types in C# Common Type System (CTS). It serves as an alias for System.Object class. All types in C# inherit from object, making it possible to assign any value to an object variable. The dynamic type bypasses compile-time type checking entirely. Operations on dynamic variables are resolved at runtime using the ...

Read More

Difference between prefix and postfix operators in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 7K+ Views

The prefix and postfix operators in C# are increment (++) and decrement (--) operators that behave differently based on their position relative to the variable. The key difference lies in when the increment or decrement occurs and what value is returned. Syntax Following is the syntax for prefix operators − ++variable; // prefix increment --variable; // prefix decrement Following is the syntax for postfix operators − variable++; // postfix increment variable--; // postfix decrement Prefix Operators The prefix operator increments or decrements the variable first, ...

Read More
Showing 1121–1130 of 1,951 articles
« Prev 1 111 112 113 114 115 196 Next »
Advertisements