Found 2587 Articles for Csharp

Const vs Static vs Readonly in C#

karthikeya Boyini
Updated on 23-Jun-2020 11:03:56

1K+ Views

ConstConstant fields are the fields that cannot be modified. At the time of declaration, you need to assign a value to it.const int a = 5;StaticIf the static modifier is applied to a class then you cannot instantiate the class using the new keyword. You can use the static keyword on methods, properties, classes, constructors, etc.static int a = 10;ReadonlyA Readonly field is initialized at the time of declaration or you can also set it within the constructor.Let us see an example in which the readonly field is initialized inside the constructor.Exampleclass Demo {    readonly int a;    public ... Read More

Compressing and Decompressing files in C#

Samual Sam
Updated on 23-Jun-2020 11:09:08

2K+ Views

Use the System.IO.Compression Namespace in C# to compress and decompress files in C#.CompressTo zip a file, use the GZipStream class with the FileStream class. Set the following parameters: File to be zipped and the name of the output zip file.Here, outputFile is the output file and the file is read into the FileStream.Exampleusing(var compress = new GZipStream(outputFile, CompressionMode.Compress, false)) {    byte[] b = new byte[inFile.Length];    int read = inFile.Read(b, 0, b.Length);    while (read > 0) {       compress.Write(b, 0, read);       read = inFile.Read(b, 0, b.Length);    } }DecompressTo decompress a file, use ... Read More

Compute modulus division by a power-of-2-number in C#

Ankith Reddy
Updated on 23-Jun-2020 11:05:24

137 Views

We have taken the number as the following −uint a = 9; uint b = 8;Above, a is a divisor and b is dividend.To compute modulus division.Example Live Demousing System; class Demo {    static uint display( uint a, uint b) {       return ( a & (b-1) );    }    static public void Main () {       uint a = 9;       uint b = 6;       Console.WriteLine( a + " modulus " + b + " = " + display(a, b));    } }Output9 modulus 6 = 1

What is the purpose of an access specifier in C#?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

520 Views

To define the scope and visibility of a class member, use an access specifier. C# supports the following access specifiers. Public Private Protected Internal Protected internal Let us learn about them one by one. Public Access Specifier It allows a class to expose its member variables and member functions to other functions and objects. Private Access Specifier Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Protected Access Specifier Protected access specifier allows a child class to ... Read More

What is the purpose of ‘is’ operator in C#?

karthikeya Boyini
Updated on 23-Jun-2020 11:10:37

250 Views

The "is" operator in C# checks whether the run-time type of an object is compatible with a given type or not.The following is the syntax.expr is typeHere, expr is the expressiontype is the name of the typeThe following is an example showing the usage of is operator in C#.Exampleusing System; class One { } class Two { } public class Demo {    public static void Test(object obj) {       One x;       Two y;       if (obj is One) {          Console.WriteLine("Class One");          x = (One)obj; ... Read More

Comparing enum members in C#

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

2K+ 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

1K+ 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

204 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

103 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

10K+ 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

Advertisements