Found 34488 Articles for Programming

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

277 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

70 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

960 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

Inheritance vs Composition in C#

Samual Sam
Updated on 22-Jun-2020 12:47:31

550 Views

InheritanceWith Inheritance, you can designate that the new class should inherit the members of an existing class. This existing class is called the baseclass, and the new class is referred to as the derived class. Inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.For example, A base class Shape has a derived classes like Circle, Square, Rectangle, etc.CompositionUnder Composition, if the parent object is deleted, then the child object also loses its status. Composition is a special type of Aggregation and gives a part-of relationship.For example, ... Read More

How to use string.Empty or String.Empty to initialize a string in C#?

karthikeya Boyini
Updated on 22-Jun-2020 12:48:03

556 Views

Set the string as empty using the string.Empty in C# −string myStr = string.Empty;To check whether it is a string or not, use the IsNullOrEmpty() method −if (string.IsNullOrEmpty(myStr)) {    Console.WriteLine("String is empty or null!"); }The following is an example −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string myStr = string.Empty;          if (string.IsNullOrEmpty(myStr)) {             Console.WriteLine("String is empty or null!");          } else {             Console.WriteLine("String isn't empty or null!");          }       }    } }OutputString is empty or null!

How to make a C# program sleep for x milliseconds?

Samual Sam
Updated on 22-Jun-2020 12:32:53

1K+ Views

To make a C# program sleep for x milliseconds, use the Thread.Sleep() method.To set it for 1000 milliseconds −Thread.Sleep(1000);The following is the code showing how to set a counter for the thread and set it to sleep for 1000 milliseconds on every iteration of for loop −Example Live Demousing System; using System.Threading; namespace MultithreadingApplication {    public class ThreadCreationProgram {       public static void CallToChildThread() {          try {             Console.WriteLine("Child thread starts");             for (int counter = 0; counter

Does declaring an array create an array in C#?

karthikeya Boyini
Updated on 22-Jun-2020 12:33:45

58 Views

Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array.The following is a declaration and it will not create an array −int[] id;The following create an array of integers. The array is a reference type, so you need to use the new keyword to create an instance of the array −Int[] id = new int[5] {};Let us see an example −Example Live Demousing System; namespace ArrayApplication {    public class MyArray {       public static void Main(string[] args) {          int ... Read More

Comparison of double and float primitive types in C#

Samual Sam
Updated on 30-Jul-2019 22:30:23

309 Views

Precision states the difference between float and double data type. Float is a single precision (32 bit) floating point data type. Double is a double precision (64 bit) floating point data type. Range of a float type − -3.4 x 1038 to + 3.4 x 1038 Range of a double type is − (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 Default value of a float type − 0.0F Default value of a double type − 0.0D

Advertisements