Found 34494 Articles for Programming

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

Arjun Thakur
Updated on 22-Jun-2020 12:53:12

2K+ Views

friend in C#A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.C++ equivalent of friend in C#The closest equivalent is to create a nested class that will access the outer class private members.Here, the inner class can ... Read More

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

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

2K+ Views

The C# equivalent for Java System.exit(0) is − Environment.Exit(exitCode); The Environment.Exit() method terminates this process and returns an exit code to the operating system. Above, use exitCode as 0 (zero) to show that the process completed successfully. Use exitCode as a non-zero number to show an error, for example − Environment.Exit(1) Return a value 1 to show that the file you want is not present Environment.Exit(2)exit Return a value 2 to indicate that the file is in an incorrect format.

How to open hidden file using C#?

George John
Updated on 22-Jun-2020 12:40:11

505 Views

To open a hidden file, first make it visible. You can do this by removing the hidden attribute set on it −FileInfo file= new FileInfo(Environment.CurrentDirectory + @"\myFile.txt"); file.Attributes &= ~FileAttributes.Hidden;Now treat it as a normal text file and open it. Read the content −using (StreamReader sr = new StreamReader("myFile.txt")) {    string line;    while ((line = sr.ReadLine()) != null) {       Console.WriteLine(line);    } }After reading, set the attribute as hidden again to hide the file −file.Attributes |= FileAttributes.Hidden;

Different ways of Reading a file in C#

Chandu yadav
Updated on 22-Jun-2020 12:43:05

219 Views

Here, we are reading two different files −Reading text file −Example Live Demousing System; using System.IO; namespace FileApplication {    class Program {       static void Main(string[] args) {          try {             using (StreamReader sr = new StreamReader("d:/new.txt")) {                string line;                // Read and display lines from the file until                // the end of the file is reached.                while ((line ... Read More

How to open a plain text file in C#?

Arjun Thakur
Updated on 22-Jun-2020 12:44:26

244 Views

To open a plain text file, use the StreamReader class. The following opens a file for reading −StreamReader sr = new StreamReader("d:/new.txt")Now, display the content of the file −while ((line = sr.ReadLine()) != null) {    Console.WriteLine(line); }Here is the code −Example Live Demousing System; using System.IO; namespace FileApplication {    class Program {       static void Main(string[] args) {          try {             using (StreamReader sr = new StreamReader("d:/new.txt")) {                string line;                // ... Read More

C# ToEven property

Ankith Reddy
Updated on 22-Jun-2020 12:44:49

55 Views

The ToEven property is used with the MidpointRounding Enumeration to round a number to the nearest even number.Declare and initialize a decimal number −decimal val = 70.45M;To rounde a number to the nearest even number −decimal.Round(val, 0, MidpointRounding.ToEven)Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       decimal val = 70.45M;       Console.WriteLine(decimal.Round(val, 0, MidpointRounding.ToEven));    } }Output70

C# Truncate Method

George John
Updated on 22-Jun-2020 12:45:15

276 Views

Use the Truncate method in C# to remove all the numbers after decimal places.Let’s say the following is our number −20.35MTo remove the numbers after decimal places, use Truncate() −decimal.Truncate(20.35M)Let us see the comple code −Exampleusing System; using System.Linq; class Demo {    static void Main() {       decimal dc = 20.35M;       Console.WriteLine(dc.Truncate(val));    } }

How to check the Existence of a File using C#?

Chandu yadav
Updated on 22-Jun-2020 12:45:42

69 Views

Let’s say we need to find the following file −E:ew.txtTo check the existence of the above file, use the Exists() method −if (File.Exists(@"E:ew.txt")) {    Console.WriteLine("File exists..."); }Here is the complete code to check the existence of a file −Example Live Demousing System; using System.IO; public class Demo {    public static void Main() {       if (File.Exists(@"E:ew.txt")) {          Console.WriteLine("File exists...");       } else {          Console.WriteLine("File does not exist in the E directory!");       }    } }OutputFile does not exist in the E directory!

How does Garbage Collector work in C#

varma
Updated on 30-Jul-2019 22:30:23

1K+ Views

The garbage collector (GC) manages the allocation and release of memory. The garbage collector serves as an automatic memory manager. You do not need to know how to allocate and release memory or manage the lifetime of the objects that use that memory. An allocation is made any time you declare an object with a “new” keyword or a value type is boxed. Allocations are typically very fast. When there is not enough memory to allocate an object, the GC must collect and dispose of garbage memory to make memory available for new allocations. This process is known as ... Read More

Local Inner Class in C#

karthikeya Boyini
Updated on 22-Jun-2020 12:46:41

955 Views

A nested class is a class declared in another enclosing class and it has inner as well as outer class. It is a member of its enclosing class and the members of an enclosing class have no access to members of a nested classLet us see an example code snippet of nested classes in C#.Here, class Two is a local inner class −Exampleclass One {    public int num1;    public class Two {       public int num2;    } } class Demo {    static void Main() {       One x = new One();   ... Read More

Advertisements