The Array.SyncRoot property is used to get an object that can be used to synchronize access to the Array. The classes that have arrays can also use the SyncRoot property to implement their own synchronization.Enumerating through a collection is not a thread safe procedure. The other threads may modify the collection even when the collection is synchronized. This would eventually cause the enumerator to throw an exception. For this, you need to lock the collection.Let us see an example to work with Array.SyncRoot property −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { ... Read More
Circular reference occurs when two or more interdependent resources cause lock condition. This makes the resource unusable.To handle the problem of circular references in C#, you should use garbage collection. It detects and collects circular references. The garbage collector begins with local and static and it marks each object that can be reached through their children.Through this, you can handle the issues with circular references.Let’s say the following classes is in circular reference. Here both of them depends on each other −public class A { B Two; } public class B { A one; }To solve the ... Read More
The using statement is used to set one or more than one resource. These resources are executed and the resource is released. The statement is also used with database operations.The main goal is to manage resources and release all the resources automatically.Let us see an example wherein “A” would print first since the SystemResource is allocated first.Example Live Demousing System; using System.Text; class Demo { static void Main() { using (SystemResource res = new SystemResource()) { Console.WriteLine("A"); } Console.WriteLine("B"); } } class SystemResource : IDisposable { public void Dispose() { Console.WriteLine("C"); } }OutputA C B
String literals or constants are enclosed in double quotes "" or with @"". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.Here are some examples of String Literals −“Hi, User" "You’re Welcome, \The following is an example showing the usage of string literals −Example Live Demousing System; namespace Demo { class Program { static void Main(string[] args) { // string string str1 ="Hello, World"; Console.WriteLine(str1); // Multi-line string string str2 = @"Welcome, Hope you are doing great!"; Console.WriteLine(str2); } } }OutputHello, World Welcome, Hope you are doing great!
A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.The following are some of the examples of floating point literals −9.23456 269485E-5FLet us now print the floating point literals −Example Live Demousing System; namespace Demo { class Program { static void Main(string[] args) { // float float a = 3.56f; Console.WriteLine(a); // float float b = 3.14159f; Console.WriteLine(b); } } }Output3.56 3.14159
The System.IO namespace has various classes useful for performing various operations with files, such as creating and deleting files, reading from or writing to a file, closing a file etc.The following are the I/O classes in C# −Sr.No.I/O Class & Description1BinaryReaderReads primitive data from a binary stream.2BinaryWriterWrites primitive data in binary format.3BufferedStreamA temporary storage for a stream of bytes.4DirectoryHelps in manipulating a directory structure.5DirectoryInfoUsed for performing operations on directories.6DriveInfoProvides information for the drives.7FileHelps in manipulating files.8FileInfoUsed for performing operations on files.9FileStreamUsed to read from and write to any location in a file.10MemoryStreamUsed for random access to streamed data stored in ... Read More
The Array.IsSynchronized property in C gets a value indicating whether access to the Array is synchronized.The IsSynchronized property is implemented by Arrays because it is needed by the System.Collections.ICollection interface. Classes using arrays can also implement own synchronization using the SyncRoot property.The following is the syntax −public bool IsSynchronized { get; }The Array.IsSynchronized property implementation is the same like the SyncRoot property −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { Array arr = new int[] { 2, 1, 9, 4, 8, 6,8 }; lock(arr.SyncRoot) { foreach (Object val in arr) Console.WriteLine(val); } } }Output2 1 9 4 8 6 8
To determine if a string has unique characters or not, firstly check a word in the string with the next word −for (int j = i + 1; j < val.Length; j++) { if (val[i] == val[j]) }If you find a match, that would mean the string do not have unique characters.If you are unable to find a match, then the string has all unique characters.In case of a match, return false i.e. unique characters not found −for (int j = i + 1; j < val.Length; j++) { if (val[i] == val[j]) return false; }
Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.A common use of the ReadKey() method is that you can halt the program execution. This could be done until a key is pressed by the user.Let us see an example −Example Live Demousing System; public class Demo { public static void Main() { DateTime date = DateTime.Now; TimeZoneInfo timeZone = TimeZoneInfo.Local; Console.WriteLine("Time Zone = {0}", timeZone.IsDaylightSavingTime(date) ... Read More
Integer LiteralsAn integer literal can be a decimal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal. Here are some of the examples of integer literals −10 // int 18u // unsigned intLet’s use the above literal while declaring and initializing a variable −// int int a =10;We will now print the values −Example Live Demousing System; namespace Demo { class Program { static void Main(string[] args) { // int int a =200; ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP