Csharp Articles

Page 127 of 196

Does declaring an array create an array in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 153 Views

Declaring an array in C# does not create the actual array object in memory. Array declaration only creates a reference variable that can point to an array object. The array must be explicitly initialized using the new keyword to allocate memory and create the array instance. Array Declaration vs Array Creation Understanding the difference between declaration and creation is crucial for working with arrays in C# − Array Declaration vs Creation Declaration Only int[] arr; • Creates reference variable • No memory ...

Read More

Local Inner Class in C#

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

A nested class in C# is a class declared inside another enclosing class. The nested class is a member of its outer class and can access the outer class's private members, while the outer class cannot directly access the nested class's members without creating an instance. Nested classes provide better organization and encapsulation by grouping related functionality together. They are particularly useful when a class is only meaningful within the context of another class. Syntax Following is the syntax for declaring a nested class − class OuterClass { // outer class ...

Read More

C# ToEven property

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 158 Views

The ToEven property is a value in the MidpointRounding enumeration that implements banker's rounding. When a number falls exactly between two integers, it rounds to the nearest even number. This method reduces bias in calculations compared to always rounding up or down. Syntax Following is the syntax for using MidpointRounding.ToEven − decimal.Round(value, digits, MidpointRounding.ToEven) Math.Round(value, digits, MidpointRounding.ToEven) Parameters value − The number to be rounded digits − Number of decimal places in the return value MidpointRounding.ToEven − Rounds to the nearest even number when the value is exactly halfway between two numbers ...

Read More

How to open a plain text file in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 354 Views

In C#, you can open and read a plain text file using the StreamReader class from the System.IO namespace. The StreamReader provides methods to read character data from a stream in a particular encoding. Syntax Following is the basic syntax for opening a text file with StreamReader − StreamReader sr = new StreamReader("filepath"); For proper resource management, use the using statement − using (StreamReader sr = new StreamReader("filepath")) { // Read file content } Using StreamReader to Read Text Files The most common approach is ...

Read More

Different ways of Reading a file in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 346 Views

There are several ways to read files in C#, each suited for different scenarios. C# provides various classes in the System.IO namespace for file operations, including reading text files, binary files, and handling different data types efficiently. Using StreamReader for Text Files The StreamReader class is the most common way to read text files line by line. It provides efficient reading with proper resource management using the using statement − using System; using System.IO; class Program { static void Main(string[] args) { // ...

Read More

How to open hidden file using C#?

George John
George John
Updated on 17-Mar-2026 781 Views

To open a hidden file in C#, you need to first make it visible by removing the hidden attribute, then read its contents, and optionally restore the hidden attribute afterward. Hidden files have the FileAttributes.Hidden attribute set, which prevents them from being displayed in normal file explorers. Syntax Following is the syntax for removing the hidden attribute from a file − FileInfo file = new FileInfo(filePath); file.Attributes &= ~FileAttributes.Hidden; Following is the syntax for restoring the hidden attribute − file.Attributes |= FileAttributes.Hidden; Using FileInfo to Remove Hidden Attribute The ...

Read More

What is the C# equivalent for the Java System.exit(0)?

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

The C# equivalent for Java's System.exit(0) is the Environment.Exit() method. This method terminates the current process and returns an exit code to the operating system, just like its Java counterpart. Syntax Following is the syntax for using Environment.Exit() − Environment.Exit(exitCode); Parameters exitCode − An integer value returned to the operating system. Use 0 to indicate successful termination, and non-zero values to indicate different error conditions. Exit Code Conventions Exit Code Meaning 0 Process completed successfully 1 General error ...

Read More

What is the C# equivalent of C++ friend keyword?

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

In C++, the friend keyword allows external functions or classes to access private and protected members of a class. C# does not have a direct equivalent to the friend keyword, but there are several approaches to achieve similar functionality. Understanding the friend Concept A friend function in C++ is defined outside a class but has access to all private and protected members of that class. While C# doesn't support this directly, it provides alternative mechanisms to control access between closely related classes. Using Nested Classes The closest equivalent to C++ friend functionality is using nested classes. ...

Read More

How to convert a JavaScript array to C# array?

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

Converting a JavaScript array to a C# array involves passing the JavaScript array data to your C# application, typically through web forms, AJAX requests, or by converting the array to a string format that C# can parse. The most common approach is to serialize the JavaScript array to a string (usually comma-separated or JSON format) and then parse it in C# to recreate the array. Using Comma-Separated String Conversion First, convert the JavaScript array to a comma-separated string using the join() method − var myArr = ["Welcome", "to", "the", "Web", "World"]; ...

Read More

What is the C# equivalent to Java's isInstance()?

George John
George John
Updated on 17-Mar-2026 947 Views

Java's isInstance() method determines if a specified object is assignment-compatible with the object represented by a class. In C#, there are several equivalent approaches to achieve the same functionality for type checking and instance validation. C# Equivalents to Java's isInstance() The most common C# equivalents are the is operator, IsAssignableFrom() method, and IsInstanceOfType() method. Each serves different use cases depending on whether you're working with objects, types, or need runtime type checking. Using the 'is' Operator The simplest and most commonly used equivalent is the is operator − bool result = (object is ClassName); ...

Read More
Showing 1261–1270 of 1,951 articles
« Prev 1 125 126 127 128 129 196 Next »
Advertisements