Csharp Articles - Page 164 of 196

How to swap two numbers without using a temp variable in C#

karthikeya Boyini
Updated on 21-Jun-2020 16:24:18

903 Views

To swap two numbers, use the third variable and perform arithmetical operator without using a temp variable.Set two variables for swapping −val1 = 5; val2 = 10;Now perform the following operation for swap −val1 = val1 + val2; val2 = val1 - val2; val1 = val1 - val2;Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          int val1,val2;          val1 = 5;          val2 = 10;          Console.WriteLine("Values before swap...");          Console.WriteLine(val1.ToString());          Console.WriteLine(val2.ToString());          val1 = val1 + val2;          val2 = val1 - val2;          val1 = val1 - val2;          Console.WriteLine("Values after swap...");          Console.WriteLine(val1.ToString());          Console.WriteLine(val2.ToString());          Console.ReadLine();       }    } }

How to reverse a String using C#?

Samual Sam
Updated on 21-Jun-2020 16:05:29

642 Views

To reverse a string, use the Array.Reverse() method.Set the string you want to reverse −string str = "Amit";In the above method, we have converted the string into character array −char[] ch = str.ToCharArray();Then the Reverse() method is used.Array.Reverse(ch);Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "Amit";          char[] ch = str.ToCharArray();          Array.Reverse(ch);          foreach(var items in ch) {             Console.WriteLine(items);          }          Console.ReadLine();       }    } }

Private Methods in C#

Chandu yadav
Updated on 21-Jun-2020 16:26:34

8K+ Views

Private Methods can only be used inside the class. To 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.The following is an example −Exampleusing System; class Demo {    private int displayOne() {       return 10;    }    public int displayTwo() {       return 10;    } } class Program {    static ... Read More

Overriding in C#

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

636 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

Mobile

Numbers in C#

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

329 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

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

Mutation Test tools in C#

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

360 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

Optional property in a C# 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; } }

Private Variables in C#

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

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

880 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.

Advertisements