Swap two variables in one line using C#

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

395 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

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

Optional property in a C# class

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

4K+ 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; } }

Overloading in C#

Samual Sam
Updated on 21-Jun-2020 16:10:33

189 Views

Overloading is of two types in C#.Function OverloadingYou 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 −public static int mulDisplay(int one, int two) { } public static int mulDisplay(int one, int two, int three) { } public static int mulDisplay(int one, int two, int three, int four) { }Operator OverloadingOverloaded operators are functions with special names. The keyword operator is followed by the symbol for the operator being defined.public ... Read More

Mutation Test tools in C#

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

238 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

Nested Classes in C#

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

414 Views

Nested class is a class declared in another enclosing class. It is a member of its enclosing class and the members of an enclosing class have no access to members of a nested class.Let us see an example code snippet of nested classes in C# −Exampleclass One {    public int val1;    public class Two {       public int val1;    } } class Demo {    static void Main() {       One a = new One();       a.val1++;       One.Two ab = new One.Two();       ... Read More

Naming Conventions in C#

Ankith Reddy
Updated on 21-Jun-2020 16:08:58

2K+ Views

Naming convetion for classesA class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces. The following are the conventions for class names.Pascal CasingThe coding conventions for a class name is the the name of the class names, for example, it should being PascalCasing.public class EmployeeDetails {}Above, the class name EmployeeDetails is in PascalCasing.Noun or Noun PhrasesPrefer adding class names as noun or noun phrases −public class Employee {} Identifier is a name used to identify a class, variable, function, or any other user-defined item.The following are the ... Read More

Null Pointer Exception in C#

Samual Sam
Updated on 21-Jun-2020 16:08:32

2K+ Views

NullReferenceException is a C# version of NullPointerException. To handle and catch it in C#, use try-catch.The below example shows that a variable is set to null and when we try to print it, it throws an exception that gets caught in the catch −Try {    a = null;    Console.WriteLine(a); }catch (NullPointerException ex) {    Console.WriteLine("Variable is Null!"); }The above will allow the exception to be caught and use catch for it.

Numbers in C#

Arjun Thakur
Updated on 21-Jun-2020 16:08:01

115 Views

For numbers in C#, use the int type. It represents an integer, which is positive or negative whole number.Let us see how to add two integers in C# using mathematical operator + −using System; using System.Linq; class Program {    static void Main() {       int x = 20;       int y = 30;       int sum = 0;       sum = x + y;       Console.WriteLine(sum);    } }Now let us learn about the order in which these mathematical operators i.e. operator precedence.Operator precedence determines the grouping ... Read More

Overriding in C#

karthikeya Boyini
Updated on 21-Jun-2020 16:07:05

383 Views

Runtime polymorphism has method overriding that is also known as dynamic binding or late binding. It is implemented by abstract classes and virtual functions. Abstract classes contain abstract methods, which are implemented by the derived class.Let us see an example of abstract classes that implement runtime polymorphism and works with Overriding −Exampleusing System; namespace PolymorphismApplication {    abstract class Shape {       public abstract int area();    }    class Rectangle: Shape {       private int length;       private int width;       public Rectangle( int a = 0, int ... Read More

Advertisements