Found 34488 Articles for Programming

How to clear screen using C#?

karthikeya Boyini
Updated on 22-Jun-2020 13:39:55

9K+ Views

Use the Console.Clear() method to clear screen and the console buffer. When the Clear method is called, the cursor automatically scrolls to the top-left corner of the window.Here, we have cleared the screen and then set the ForegroundColor and BackgroundColor −ConsoleColor newForeColor = ConsoleColor.Blue; ConsoleColor newBackColor = ConsoleColor.Yellow;The following is the complete code −Example Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       ConsoleColor foreColor = Console.ForegroundColor;       ConsoleColor backColor = Console.BackgroundColor;       Console.WriteLine("Clearing the screen!");       Console.Clear();       ConsoleColor newForeColor = ConsoleColor.Blue;       ConsoleColor newBackColor = ConsoleColor.Yellow;    } }OutputClearing the screen!

Scope of Variables in C#

Samual Sam
Updated on 22-Jun-2020 13:25:45

703 Views

The scope of a variable is a region of code that indicates where the variables are being accessed.For a variable, it has the following levels −Method LevelVariable declared inside a method is a local variable.Class LevelVariable declared inside a class is a local variable are class member variables.Let us see an example of scope of variables −Example Live Demousing System; namespace Demo {    class Program {       public int Divide(int num1, int num2) {          // local variable in a method          int result;          result = num1 ... Read More

Pair Class in C#

Ankith Reddy
Updated on 22-Jun-2020 13:20:45

4K+ Views

The Pair class is the KeyValuePair class that stores a pair of values in a single list with C#.Declare KeyValuePair −var myList = new Liststring, int>>(); Now, add some elements: myList.Add(new KeyValuePair("Laptop", 1)); myList.Add(new KeyValuePair("Desktop System", 2)); myList.Add(new KeyValuePair("Tablet", 3)); myList.Add(new KeyValuePair("Mobile", 4)); myList.Add(new KeyValuePair("E-Book Reader", 5)); myList.Add(new KeyValuePair("LED", 6));Display the KeyValuePair now as shown below −Example Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       var myList = new List();       // adding elements       myList.Add(new KeyValuePair ("Laptop", 1));       myList.Add(new KeyValuePair ("Desktop System", 2));   ... Read More

File Permissions in C#

George John
Updated on 22-Jun-2020 13:21:18

2K+ Views

For File Permission in C#, use the FileIOPermission Class. It controls the ability to access files and folders.The following are the properties of File Permissions class −Sr.No.Methods & Description1AllFilesGets or sets the permitted access to all files.2AllLocalFilesGets or sets the permitted access to all local files.The following are the methods of File Permission class −Sr.No.Methods & Description1AddPathList(FileIOPermissionAccess, String)This method adds access for the specified file or directory to the existing state of the permission2Copy()This method creates and returns an identical copy of the current permission.3GetType()The GetType() method gets the type of the current instance.4ToXml()Creates an XML encoding of the permission ... Read More

Difference between Dictionary and Hashtable in C#

Chandu yadav
Updated on 22-Jun-2020 13:22:20

493 Views

Hashtable is slower than Dictionary. For strongly-typed collections, the Dictionary collection is faster.HashtableHashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.Let us see an example −Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          Hashtable ht = new Hashtable();          ht.Add("E001", "Tom");          ht.Add("E098", "Amit");          ht.Add("E110", "Jack");         ... Read More

C# program to find if an array contains duplicate

karthikeya Boyini
Updated on 22-Jun-2020 13:24:00

1K+ Views

Set an array −int[] arr = {    89,    12,    56,    89, };Now, create a new Dictionary −var d = new Dictionary < int, int > ();Using the dictionary method ContainsKey(), find the duplicate elements in the array −foreach(var res in arr) {    if (d.ContainsKey(res))    d[res]++;    else    d[res] = 1; }Here is the complete code −Example Live Demousing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          int[] arr = {             89, ... Read More

C# program to convert an array to an ordinary list with the same items

Samual Sam
Updated on 22-Jun-2020 13:24:36

74 Views

Set an array −int[] arr = { 23, 66, 96, 110 };Now, create a new list −var list = new List();Use the Add method and add the array elements to the list −for (int i = 0; i < arr.Length; i++) {    list.Add(arr[i]); }The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       int[] arr = { 23, 66, 96, 110 };       var list = new List();       for (int i = 0; i < arr.Length; i++) {          list.Add(arr[i]);       }       foreach(int res in list) {          Console.WriteLine(res);       }    } }Output23 66 96 110

C# program to determine if Two Words Are Anagrams of Each Other

karthikeya Boyini
Updated on 22-Jun-2020 13:25:04

6K+ Views

For anagram, another string would have the same characters present in the first string, but the order of characters can be different.Here, we are checking the following two strings −string str1 = "heater"; string str2 = "reheat";Convert both the strings into character array −char[] ch1 = str1.ToLower().ToCharArray(); char[] ch2 = str2.ToLower().ToCharArray();Now, sort them −Array.Sort(ch1); Array.Sort(ch2);After sorting, convert them to strings as shown in the following code −Example Live Demousing System; public class Demo {    public static void Main () {       string str1 = "heater";       string str2 = "reheat";       char[] ch1 ... Read More

What is the difference between virtual and abstract functions in C#?

Samual Sam
Updated on 22-Jun-2020 13:12:21

4K+ Views

Abstract methods do not provide an implementation and they force the derived classes to override the method. It is declared under abstract class. An abstract method only has the method definitionVirtual methods have an implementation, unlike the Abstract method and it can exist in the abstract and non-abstract class. It provides the derived classes with the option of overriding it.Virtual FunctionsThe virtual keyword is useful in modifying a method, property, indexer, or event. When you have a function defined in a class that you want to be implemented in an inherited class(es), you use virtual functions. The virtual functions could ... Read More

How to access array elements using a pointer in C#?

karthikeya Boyini
Updated on 03-Apr-2020 09:08:18

3K+ Views

In C#, an array name and a pointer to a data type same as the array data, are not the same variable type. For example, int *p and int[] p, are not the same type. You can increment the pointer variable p because it is not fixed in memory but an array address is fixed in memory, and you can't increment that.Here is an example −Exampleusing System; namespace UnsafeCodeApplication {    class TestPointer {       public unsafe static void Main() {          int[] list = {5, 25};          fixed(int *ptr = ... Read More

Advertisements