Packages in C#

Ankith Reddy
Updated on 21-Jun-2020 16:23:18

2K+ Views

As an alternative of Packages in Java, the C# language has namespace.Packages in JavaPackages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.Namespace in C#A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.A namespace definition begins with the keyword namespace followed by the namespace name. The following shows how to work with namespace in C# −Exampleusing System; ... Read More

Trim Leading and Trailing Spaces in C#

Samual Sam
Updated on 21-Jun-2020 16:22:39

900 Views

To trim a string in C#, use regular expression.Firstly, set the pattern for regex −string pattern = "\s+";Let’s say the following is our string with leading and trailing spaces −string input = " Welcome User ";Now using Regex, set the pattern and get the result in a new string in C#.Regex rgx = new Regex(pattern); string result = rgx.Replace(input, replacement);The following is the complete example −Exampleusing System; using System.Text.RegularExpressions; namespace Demo {    class Program {       static void Main(string[] args) {          string input = " Welcome User ";         ... Read More

Try Keyword in C#

Arjun Thakur
Updated on 21-Jun-2020 16:21:17

315 Views

A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.try { }With that, you need to set catch statement as well to catch the exception −try {       // statements causing exception    } catch( ExceptionName e1 ) {       // error handling code    }The following is an example −Exampleclass Demo {    int result;    Demo() {       result = 0;    }    public void division(int val1, int val2) {       try {   ... Read More

Timer in C#

karthikeya Boyini
Updated on 21-Jun-2020 16:15:33

3K+ Views

The namespace used to set a timer is System. Timers. The Timer class generates an event after a set interval, with an option to generate recurring events.Firstly, create a timer object for 5 seconds interval −timer = new System.Timers.Timer(5000);Set elapsed event for the timer. This occurs when the interval elapses −timer.Elapsed += OnTimedEvent;Now start the timer.timer.Enabled = true;Exampleusing System; using System.Timers; public class Demo {    private static Timer timer;    public static void Main() {       timer = new System.Timers.Timer();       timer.Interval = 5000;       timer.Elapsed += OnTimedEvent;     ... Read More

Tasks in Chash

Ankith Reddy
Updated on 21-Jun-2020 16:14:47

3K+ Views

Task represents an asynchronous operation in C#. The following states how you can start a task in C#.Use a delegate to start a task.Task t = new Task(delegate { PrintMessage(); }); t.Start();Use Task Factory to start a task.Task.Factory.StartNew(() => {Console.WriteLine("Welcome!"); });You can also use Lambda.Task t = new Task( () => PrintMessage() ); t.Start();The most basic way to start a task is using the run().Exampleusing System; using System.Threading.Tasks; public class Example {    public static void Main() {       Task task = Task.Run( () => {          int a = 0;          for (a = 0; a

Swap Two Strings Without Using Temp Variable in C#

Samual Sam
Updated on 21-Jun-2020 16:14:17

2K+ Views

To swap two strings without using a temp variable, you can try the following code and logic.Append the second string with the first.str1 = str1 + str2;Set the str1 in str2.str2 = str1.Substring(0, str1.Length - str2.Length);Now, the final step is to set str2 in str1 −str1 = str1.Substring(str2.Length);Exampleusing System; class Demo {    public static void Main(String[] args) {       String str1 = "Brad";       String str2 = "Pitt";       Console.WriteLine("Strings before swap");       Console.WriteLine(str1);       Console.WriteLine(str2);       str1 = str1 + str2; ... Read More

Swap Two Variables in One Line Using C#

George John
Updated on 21-Jun-2020 16:13:35

594 Views

To swap two variables in a single line using the Bitwise XOR Operator.val1 = val1 ^ val2 ^ (val2 = val1);Above, we have set the values −int val1 = 30; int val2 = 60;The following is the example to swap both the variable in one line using C# −Exampleusing System; class Demo {    public static void Main(String[] args) {       int val1 = 30;       int val2 = 60;       Console.WriteLine("Values before swap");       Console.WriteLine(val1);       Console.WriteLine(val2);       val1 = val1 ^ val2 ^ (val2 = val1);       Console.WriteLine("Values after swap");       Console.WriteLine(val1);       Console.WriteLine(val2);    } }

Private Variables in C#

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

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

Optional Property in a Chash Class

Chandu yadav
Updated on 21-Jun-2020 16:11:09

5K+ Views

A property is optional if it is possible and valid for it to have null. A property whose CLR type cannot have null cannot be configured optional.An example optional attribute usage −Example[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)] internal sealed class OptionalAttribute : Attribute { } public class Employee {    public string EmpName { get; set; }    [Optional]    public string AlternativeName { get; set; } }

Mutation Test Tools in C#

karthikeya Boyini
Updated on 21-Jun-2020 16:10:08

340 Views

One of the best tools for mutation testing in C# is “VisualMutator” It is integrated with the .NET programming environment.The following are the features of VisualMutant, which is a mutation test tool −Measure the quality of the test suite.To create first-order mutants using built-in and custom mutation operators.View modified code fragments in C#.Run NUnit and XUnit tests on generated mutants.Provides information about passed and failed testsYou can also write the results to XML.View details about any mutant right after the start of the mutation testing processIt gives results as mutation score

Advertisements