Found 2628 Articles for Csharp

What are identifiers in C#?

Arjun Thakur
Updated on 20-Jun-2020 16:37:21

773 Views

An identifier is a name used to identify a class, variable, function, or any other user-defined item. The basic rules for naming classes in C# are as follows −A name must begin with a letter that could be followed by a sequence of letters, digits (0 - 9) or underscore. The first character in an identifier cannot be a digit.It must not contain any embedded space or symbol such as? - + ! @ # % ^ & * ( ) [ ] { } . ; : " ' / and \. However, an underscore ( _ ) can ... Read More

What are key-based I/O collections in C#?

Chandu yadav
Updated on 20-Jun-2020 16:38:04

141 Views

The key-based I/O Collections in C# is what we call a SortedList −SortedListThe SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index. This is how both of them gets added in a SortedList −s.Add("Sub1", "Physics"); s.Add("Sub2", "Chemistry"); s.Add("Sub3", "Biology"); s.Add("Sub4", "Java");The following is an example displaying the keys and values in a SortedList −Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          SortedList s = new SortedList();       ... Read More

What are generic delegates in C#?

George John
Updated on 20-Jun-2020 16:41:07

710 Views

Using generic delegates, you do not need to define the delegate statement. They are defined in the System namespace.You can define a generic delegate with type parameters. For example −delegate T myDelegete(T n);ExampleThe following is an example showing how to create generic delegates in C# −using System; using System.Collections.Generic; delegate T myDelegete(T n); namespace GenericDelegateAppl {    class TestDelegate {       static int num = 5;       public static int AddNum(int p) {          num += p;          return num;       }       ... Read More

What are generic collections in C#?

Ankith Reddy
Updated on 20-Jun-2020 16:42:29

526 Views

Generic collections in C# include , , etc.ListList is a generic collection and the ArrayList is a non-generic collection.Let us see an example. Here, we have six elements in the list −Example Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       // Initializing collections       List myList = new List() {          "one",          "two",          "three",          "four",          "five",          "six"       };       Console.WriteLine(myList.Count); ... Read More

Sort list of dictionaries by values in C#

Arjun Thakur
Updated on 20-Jun-2020 16:42:53

985 Views

Firstly, let us create a dictionary −var d = new Dictionary(5);Now add the key and value −// add key and value d.Add("car", 25); d.Add("bus", 28); d.Add("motorbike", 17);Use orderby to order by values −var val = from ele in d orderby ele.Value ascending select ele;We have set ascending above to sort the dictionary in ascending order. You can also use descending.Display the values in ascending order −foreach (KeyValuePair ele in val) {    Console.WriteLine("{0} = {1}", ele.Key, ele.Value); }

What are I/O classes in C#?

Chandu yadav
Updated on 20-Jun-2020 16:22:55

640 Views

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

What are floating point literals in C#?

Ankith Reddy
Updated on 20-Jun-2020 16:27:44

793 Views

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

What are string literals in C#?

Arjun Thakur
Updated on 20-Jun-2020 16:28:38

298 Views

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!

What is the use of ‘Using’ statement in C#?

George John
Updated on 20-Jun-2020 16:29:25

4K+ Views

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

What are circular references in C#?

Ankith Reddy
Updated on 20-Jun-2020 16:30:28

3K+ Views

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

Advertisements