A namespace inside a namespace is called a nested namespace in C#. This is mainly done to properly structure your code.We have an outer namespace −namespace outer {}Within that, we have an inner namespace inside the outer namespace −namespace inner { public class innerClass { public void display() { Console.WriteLine("Inner Namespace"); } } }Now to call the method of inner namespace, set a class object of the inner class and call the method as shown in the below example −namespace outer { class Program { ... Read More
A process is an active program i.e a program that is under execution. It contains the program code, program counter, process stack, registers etc.Process StatesThe different states that a process is in during its execution are explained using the following diagram −New- The process is in new state when it has just been created.Ready - The process is waiting to be assigned the processor by the short term scheduler.Running - The process instructions are being executed by the processor.Waiting - The process is waiting for some event such as I/O to occur.Terminated - The process has completed its execution.Process Control ... Read More
Ternary operator is a Conditional operator in C#. It takes three arguments and evaluates a Boolean expression.For example −y = (z == 1) ? 100 : 180;Above, if the first operand evaluates to true (1), the second operand is evaluated. If the first operand evaluates to false (0), the third operand is evaluated.The following is an example −Exampleusing System; namespace Demo { class Program { static void Main(string[] args) { int x, y; x = 25; y = (x == 25) ? 20 : 30; Console.WriteLine("Value of x = {0}", y); y = (x == 1) ? 50 : 90; Console.WriteLine("Value of y = {0}", y); Console.ReadLine(); } } }Above we have two conditions using the ternary operators −y = (x == 25) ? 20 : 30; y = (x == 1) ? 50 : 90;
A lambda expression in C# describes a pattern. It has the token => in an expression context. This is read as “goes to” operator and used when a lambda expression is declared.The following is an example showing how to use lambda expressions in C# −Example Live Demousing System; using System.Collections.Generic; class Demo { static void Main() { List list = new List() { 21, 17, 40, 11, 9 }; int res = list.FindIndex(x => x % 2 == 0); Console.WriteLine("Index: "+res); } }OutputIndex: 2Above, we saw the usage of ... Read More
HashtableA hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.The members in a Hashtable are thread safe. It returns null if we try to find a key that does not exist. Hashtable is not a generic type.The Hashtable collection is slower than dictionary because it requires boxing and unboxing.To declare a Hashtable −Hashtable ht = new Hashtable();DictionaryDictionary is a collection of keys and values in C#. Dictionary ... Read More
Firstly, set a character−char ch = 'K';Now simply increment it like this −ch++;If you will print the character now, it would be the next character as shown in the following example −Example Live Demousing System; using System.Collections.Generic; class Demo { static void Main() { char ch = 'K'; Console.WriteLine("Initial character:"+ch); // increment character ch++; Console.WriteLine("New character:"+ch); } }OutputInitial character:K New character:L
The following are the escape characters in C# and the display column suggests how to use and print them in C# −Escape CharacterDescriptionPatternDisplay\aMatches a bell character, \u0007.\a"\u0007" in "Warning!" + '\u0007'\bIn a character class, matches a backspace, \u0008.[\b]{3, }"\b\b\b\b" in "\b\b\b\b"\tMatches a tab, \u0009.(\w+)\t"Name\t", "Addr\t" in "Name\tAddr\t"\rMatches a carriage return, \u000D. (\r is not equivalent to the newline character, .)\r(\w+)"\rHello" in "\r\HelloWorld."\vMatches a vertical tab, \u000B.[\v]{2, }"\v\v\v" in "\v\v\v"\fMatches a form feed, \u000C.[\f]{2, }"\f\f\f" in "\f\f\f"Matches a new line, \u000A.\r(\w+)"\rHello" in "\r\HelloWorld."\eMatches an escape, \u001B.\e"\x001B" in "\x001B"nnUses octal representation to specify a character (nnn consists of up to three digits).\w\040\w"a ... Read More
Use the ReadLine() method to read input from the console in C#. This method receives the input as string, therefore you need to convert it.For example −Let us see how to get user input from user and convert it to integer.Firstly, read user input −string val; Console.Write("Enter integer: "); val = Console.ReadLine();Now convert it to integer −int a = Convert.ToInt32(val); Console.WriteLine("Your input: {0}", a);Let us see this in an example. The input is added using command line argument−Example Live Demousing System; using System.Collections.Generic; class Demo { static void Main() { string val; Console.Write("Enter ... Read More
An 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. It can also have a suffix that is a combination of U and L, for unsigned and long, respectively.Here are some of the examples of integer literals −200 // int 90u// unsigned intLet’s use the above literal while declaring and initializing a variable −// int int a =200;We will now print the values −Example Live Demousing System; namespace Demo { class Program { static ... Read More
C# has the following file operations −Create, open, read and write a file.Append, Delete, etc.The FileStream class in the System.IO namespace helps in reading from, writing to and closing files. This class derives from the abstract class Stream.You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows −FileStream = new FileStream( , , , );Here, the file operations are also included as shown below −The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are −Append − It ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP