Found 34494 Articles for Programming

Comparing enum members in C#

Chandu yadav
Updated on 23-Jun-2020 11:11:59

1K+ Views

To compare enum members, use the Enum.CompareTo() method.Firstly, set the values for students.enum StudentRank { Tom = 3, Henry = 2, Amit = 1 };Now use the compareTo() method to compare one enum value with another.Console.WriteLine( "{0}{1}", student1.CompareTo(student2) > 0 ? "Yes" : "No", Environment.NewLine );The following is the code to compare enum members in C#.Example Live Demousing System; public class Demo {    enum StudentRank { Tom = 3, Henry = 2, Amit = 1 };    public static void Main() {       StudentRank student1 = StudentRank.Tom;       StudentRank student2 = StudentRank.Henry;       StudentRank ... Read More

char vs string keywords in C#

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

989 Views

string keyword Use the string keyword to declare a string variable. The string keyword is an alias for the System.String class. For example. string name; name = "Tom Hanks"; Another example. string [] array={ "Hello", "From", "Tutorials", "Point" }; char keyword The char keyword is used to set array of characters. For example. char[] ch = new char[2]; ch[0] = 'A'; // Character literal ch[1] = 'B'; // Character literal Another example. char []letters= { 'H', 'e', 'l', 'l','o' };

Check if both halves of the string have same set of characters in C#

George John
Updated on 23-Jun-2020 10:00:49

157 Views

Firstly, set the string to be checked.string s = "timetime";Now set two counters for two halves of the string.int []one = new int[MAX_CHAR]; int []two = new int[MAX_CHAR];Check for both the halves of the string.for (int i = 0, j = l - 1; i < j; i++, j--) {    one[str[i] - 'a']++;    two[str[j] - 'a']++; }The following is the complete code to check whether both the halves of the string have same set of characters or not in C#.Example Live Demousing System; class Demo {    static int MAX_CHAR = 26;    static bool findSameCharacters(string str) {   ... Read More

What is the IsFixedSize property of Hashtable class in C#?

karthikeya Boyini
Updated on 23-Jun-2020 10:01:24

69 Views

Use the isFixedSize property of Hashtable class to get a value indicating whether the Hashtable has a fixed size.The following is an example showing how to work with IsFixedSize property.Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          Hashtable ht = new Hashtable();          ht.Add("D01", "Finance");          ht.Add("D02", "HR");          ht.Add("D03", "Operations");          Console.WriteLine("IsFixedSize = " + ht.IsFixedSize);          Console.ReadKey();       }    } }OutputIsFixedSize = FalseAbove we ... Read More

Check if a File exists in C#

Samual Sam
Updated on 23-Jun-2020 10:02:16

9K+ Views

Use the File.exists method in C# to check if a file exits in C# or not.Firstly, check whether the file is present in the current directory.if (File.Exists("MyFile.txt")) {    Console.WriteLine("The file exists."); }After that check whether the file exist in a directory or not.if (File.Exists(@"D:\myfile.txt")) {    Console.WriteLine("The file exists."); }Let us see the complete example to check if a file exists in C#.Example Live Demousing System; using System.IO; class Demo {    static void Main() {       if (File.Exists("MyFile.txt")) {          Console.WriteLine("File exists...");       } else {          Console.WriteLine("File does ... Read More

Check if a File is hidden in C#

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

632 Views

To retrieve the attributes of a file, use the FileAttributes Eumeration. It has various members like compressed, directory, hidden, etc. To check if a file is hidden, use the hidden member name. If the FileAttributes.hidden is set that would mean the file is hidden. Firstly, get the path to find the attributes. FileAttributes attributes = File.GetAttributes(path); If the following is set, that would mean the file is now hidden using the hidden member name. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden); Console.WriteLine("The {0} file is hidden.", path);

What is the IsReadOnly property of SortedList class in C#?

Arjun Thakur
Updated on 23-Jun-2020 10:03:48

71 Views

Use the IsReadOnly property to get a value indicating whether the SortedList is read-only or not.You can try to run the following code to implement IsReadOnly property in C#.Here, we have set the SortedList first.SortedList s = new SortedList();Added elements.s.Add("S001", "Jack"); s.Add("S002", "Henry");Now check for IsReadOnly.Console.WriteLine("IsReadOnly = " + s.IsReadOnly);The following is the complete code.Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          SortedList s = new SortedList();          s.Add("S001", "Jack");          s.Add("S002", "Henry");          Console.WriteLine("IsReadOnly ... Read More

Character constants vs String literals in C#

karthikeya Boyini
Updated on 23-Jun-2020 10:03:16

356 Views

Character constantsCharacter literals are enclosed in single quotes. For example, 'x' and can be stored in a simple variable of char type. A character literal can be a plain character (such as 'x'), an escape sequence (such as '\t'), or a universal character (such as '\u02C0').Certain characters in C# are preceded by a backslash. They have special meaning and they are used to represent like newline () or tab (\t).Example Live Demousing System; namespace Demo {    class MyApplication {       static void Main(string[] args) {          Console.WriteLine("Welcome\t to the website");          Console.ReadLine(); ... Read More

Chaining comparison operators in C#

Chandu yadav
Updated on 30-Jul-2019 22:30:23

390 Views

C# has many operators that work on the left-right and righ-left associativity. Chanining depends on the left-to-right associativity on operators with same precedence. Operator precedence determines the grouping of terms in an expression. This affects evaluation of an expression. Certain operators have higher precedence than others do; for example, the multiplication operator has higher precedence than the addition operator. The operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators are evaluated first. To check whether a string is null or not, you can ... Read More

Draw an ellipse in C#

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

2K+ Views

To draw an ellipse, use the drawEllipse() method in C# that belong to the Graphics object. It has a pen object as well as a rectangle object. You need windows form to draw shapes in C#. Set graphics object. Graphics g = this.CreateGraphics(); Now, the pen object. Pen p = new Pen(new SolidBrush(Color.Red), 15); The following is the rectangle object. Rectangle r = new Rectangle(120, 60, 180, 180); Now use the drawEllipse() method with the graphics object and add both the objects in it to draw an ellipse. s.DrawEllipse(p, r);

Advertisements