Found 2628 Articles for Csharp

Private Variables in C#

karthikeya Boyini
Updated on 21-Jun-2020 16:12:36

5K+ Views

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. Even an instance of a class cannot access its private members.Create a private variable −private double length;Let us see an example. Here, if we will try to access the length variable which is set private, then the following error would generate.BoxApplication.Box.length' is inaccessible due to its protection levelLet us see the complete example now −Exampleusing System; namespace BoxApplication {    class Box {       private double length; ... Read More

Optimization Tips for C# Code

Arjun Thakur
Updated on 21-Jun-2020 15:57:01

722 Views

The following are the tips −Prefer ListUse List whenever necessary. Working with ArrayList for the same work can make the working of code slower. This is specially if you are storing multiple types of objects within the same list.Use multiplication-shift operationPrefer multiplication-shift operation instead of division operator, since the usage of division operator slows the code.Less code takes less memoryTry to get work done using operator to concise the code and make it work in a single line.Use operators like && that would allow you to mention all the conditions in a single line.

Operator Functions in C#

Ankith Reddy
Updated on 21-Jun-2020 15:59:26

137 Views

Operator functions are overloaded operator, which are the functions with special names. To create it, the keyword operator is followed by the symbol for the operator being defined.Like any other function, an overloaded operator has a return type and a parameter list.For example −public static Box operator+ (Vehicle v1, Vehicle v2, Vehicle v3) { }The following is the complete example showing how operator functions are created and used in C# −Exampleusing System; namespace OperatorOvlApplication {    class Box {       private double length; // Length of a box       private double breadth; // Breadth of ... Read More

Private Constructors and Singleton Classes in C#

Samual Sam
Updated on 21-Jun-2020 16:00:43

743 Views

A private constructor is used in classes containing only static member as shown below −class Demo {    // private constructor    private Demo() { }    public static a = 10; }A singleton class has normal methods and you can call it using an instance.To prevent multiple instances of the class, the private constructor is used.Let us see an example −Examplepublic class Singleton {    static Singleton a = null;    private Singleton() {    } }

Private and final methods in C#

George John
Updated on 21-Jun-2020 16:02:09

891 Views

Private MethodsTo set private methods, use the 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. Even an instance of a class cannot access its private members.Final MethodsFor final methods, use the sealed modifier.When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.Let us see an example −The following example won’t ... Read More

Priority Queues with C#

karthikeya Boyini
Updated on 21-Jun-2020 16:01:25

218 Views

A priority queue is held information with a priority value. It is an extension of queue.The item with the highest property is removed first when you try to eliminate an item from a priority queue.Let us see how to set priority queue −public class MyPriorityQueue where T : IComparable { }Now let us add an item. In the below example the items get stored in info, which is a generic list.Examplepublic class MyPriorityQueue where T : IComparable {    private List info;    public MyPriorityQueue() {       this.info = new List ();    } }

Print Single and Multiple variable in C#

Samual Sam
Updated on 21-Jun-2020 16:03:23

8K+ Views

To display single variable value in C#, you just need to use Console.WriteLine()Let us see an example. Here, we have displayed the value of a single variable “a” in a line −Exampleusing System; using System.Linq; class Program {    static void Main() {       int a = 10;       Console.WriteLine("Value: "+a);    } }To display multiple variables value in C#, you need to use the comma operator in Console.WriteLine().Let us see an example. Here, we have displayed the value of multiple variables “a” and “b” in a line −Exampleusing System; using System.Linq; class Program ... Read More

Print with your own font using C#

Chandu yadav
Updated on 21-Jun-2020 16:03:42

318 Views

To print your own font in C#, firstly construct −FontFamily objectFont ObjectThe FontFamily object sets the typeface like Arial, TimesNewRoman, etc, whereas the Font object sets the size and style of font.Let us create an Arial font style.FontFamily myFontFamily = new FontFamily("Arial"); Font myFont = new Font( myFontFamily, 20, FontStyle.Bold, GraphicsUnit.Pixel);Above, we have set the FontFamily object. The first parameter passed to the Font() is the FontFamily object “myFontFamily”, then comes the size of the font. The third argument sets the style.

Mutation Testing in C#

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

184 Views

Mutational testing in C# includes verifying the quality of a test suite in the active solution. For this, use a tool called “VisualMutant”. It sets as an extension to the Visual Studio IDE. The following are the capabilities of a testing tool. The following are the features of VisualMutant, which is a mutation test tool − View modified code fragments in C#. Run NUnit and XUnit tests on generated mutants View details about any mutant right after the start of the mutation testing process It gives results as mutation score. Measure the quality of the test suite. To create ... Read More

Overloaded method and ambiguity in C#

George John
Updated on 21-Jun-2020 16:04:20

692 Views

With method overloading, you can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.Let us see an example. In this the call would go to the method with a single parameter −Exampleusing System; class Student {    static void DisplayMarks(int marks1 = 90) {       Console.WriteLine("Method with one parameter!");    }    static void DisplayMarks(int marks1, int marks2 = 95) {       Console.WriteLine("Method with two parameters!");    } ... Read More

Advertisements