Found 2587 Articles for Csharp

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 (Remove leading and trailing spaces) a string in C#

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

878 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

Networking in C#

George John
Updated on 21-Jun-2020 16:25:00

2K+ Views

The .NET Framework has a layered, extensible, and managed implementation of networking services. You can easily integrate them into your applications. Use the System.Net; namespace.Let us see how to acess the Uri class:.In C#, it provides object representation of a uniform resource identifier (URI) −Uri uri = new Uri("http://www.example.com/"); WebRequest w = WebRequest.Create(uri);Let us now see the System.Net class. This is used to encorypt connections using using the Secure Socket Layer (SSL). If the URI begins with "https:", SSL is used; if the URI begins with "http:", an unencrypted connection is used.The following is an example. For SSL with FTP, ... Read More

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

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

828 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

591 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

587 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

Numbers in C#

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

283 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

Null Pointer Exception in C#

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

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

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

Advertisements