Found 34494 Articles for Programming

What are all the possible C# array initialization syntaxes?

Arjun Thakur
Updated on 23-Jun-2020 12:16:46

77 Views

Array can be initialized in more than one ways in C#. Let us see some example.Method OneUsing size of array.int [] marks = new int[5] { 99, 98, 92, 97, 95};Method TwoBy omitting the size.int [] marks = new int[] { 99, 98, 92, 97, 95};Method ThreeInitializing at the time of declaration.int [] marks = { 99, 98, 92, 97, 95};Let us see one of the ways to initialize arrays in C#.Example Live Demousing System; namespace Demo {    class MyArray {       static void Main(string[] args) {          int [] n = new int[10]; /* ... Read More

What are Add, Remove methods in C# lists?

karthikeya Boyini
Updated on 23-Jun-2020 12:17:39

678 Views

The List is a collection in C# and is a generic collection. The add and remove methods are used in C# lists for adding and removing elements.Let us see how to use Add() method in C#.Example Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       List sports = new List();       sports.Add("Football");       sports.Add("Tennis");       sports.Add("Soccer");       foreach (string s in sports) {          Console.WriteLine(s);       }    } }OutputFootball Tennis SoccerLet us see how to use Remove() method in C#.Example Live ... Read More

What are accessors of properties in C#?

Chandu yadav
Updated on 23-Jun-2020 12:18:13

640 Views

Properties are an extension of fields and are accessed using the same syntax. They use accessors through which the values of the private fields can be read, written or manipulated.The accessor of a property contains the executable statements that helps in getting (reading or computing) or setting (writing) the property.Let us see an example of properties in C#.ExampleDeclare a code property of type string.public string Code {    get {       return code;    }    set {       code = value;    } }In the same way, declare Age property of type as in the ... Read More

What are the differences between public, protected and private access specifiers in C#?

Samual Sam
Updated on 23-Jun-2020 12:20:52

2K+ Views

Public Access SpecifierPublic access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.Example Live Demousing System; namespace Demo {    class Rectangle {       public double length;       public double width;       public double GetArea() {          return length * width;       }         public void Display() {          Console.WriteLine("Length: {0}", length);          Console.WriteLine("Width: {0}", width);          Console.WriteLine("Area: {0}", GetArea()); ... Read More

What are abstract properties in C#?

George John
Updated on 23-Jun-2020 12:22:10

642 Views

An implementation of the property accessors will not be provided by an abstract property declaration.Let us see how to learn how to work with abstract properties. Here we have an abstract class Shape with two derived classes: Square and Circle.Here, we have an abstract Area property.The following is the Circle class.Examplepublic class Circle : Shape {    private int radius;    public Circle(int radius, string id) : base(id) {       this.radius = radius;    }    public override double Area {       get {          return radius * radius * System.Math.PI;     ... Read More

What are access specifiers in C#.NET?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

552 Views

To define the scope and visibility of a class member, use an access specifier. C# supports the following access specifiers − Public Private Protected Internal Protected internal Let us learn about them one by one. Public Access Specifier It allows a class to expose its member variables and member functions to other functions and objects. 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. Protected Access Specifier Protected access specifier allows a child class ... Read More

What are abstract classes in C#?

karthikeya Boyini
Updated on 23-Jun-2020 12:23:37

260 Views

Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.The following is an example showing the usage of abstract classes in C#.Example Live Demousing System; namespace Demo {    abstract class Shape {       public abstract int area();    }    class Rectangle: Shape {       private int length;       private int width;       public Rectangle( int a = 0, int b = 0) {          length = a;          width = b;          Console.WriteLine("Length ... Read More

How to initialize elements in an array in C#?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

157 Views

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. To initialize an array, first you need to declare it. int[] marks; Here, int is the datatype [] specifies the size of the array Marks is the name of the array Now let us initialize the array using the new keyword. int[] marks = new int[10]; Now let us assign elements to it. marks[0] = 96; marks[1] = 90 You can also assign elements like this − int [] marks = new int[10] { 78, 67, 88, 56, 90, 77, 76, 70, 60, 70};

How to create user defined exceptions in C#?

Samual Sam
Updated on 23-Jun-2020 12:07:05

233 Views

Like any other programming language, in C#, you can easily create user-defined exception. User-defined exception classes are derived from the Exception class.In the below example, the exception created is not a built-in exception.TempIsZeroExceptionYou can try to run the following code to learn how to create user defined exception in C#.Example Live Demousing System; namespace Demo {    class TestTemperature {       static void Main(string[] args) {          Temperature temp = new Temperature();          try {             temp.showTemp();          } catch(TempIsZeroException e) {       ... Read More

How to create nested while loop in C#?

George John
Updated on 23-Jun-2020 12:06:18

269 Views

For a nested while loop, we have two while loops.The first loop checks for a condition and if the condition is true, it goes to the inner loop i.e. the nested loop.Loop 1while (a

Advertisements