ArrayList class represents an ordered collection of an object that can be indexed individually. It is an alternative to an array.The following table lists some of the commonly used properties of the ArrayList class −Sr.NoProperty & Description1CapacityGets or sets the number of elements that the ArrayList can contain.2CountGets the number of elements actually contained in the ArrayList.3IsFixedSizeGets a value indicating whether the ArrayList has a fixed size.4IsReadOnlyGets a value indicating whether the ArrayList is read-only.5ItemGets or sets the element at the specified index.The following is an example showing how to work with ArrayList in C# and finding the capacity. The ... Read More
To add integer values to a list in C#, use the Add() method.Firstly, declare an integer list in C# −List list1 = new List();Now add integer values −list1.Add(900); list1.Add(400); list1.Add(300);Let us see the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo { public class Program { public static void Main(String[] args) { List list1 = new List(); list1.Add(900); list1.Add(400); list1.Add(300); Console.WriteLine(list1.Count); } } }
Firstly, declare a list −var teams = new List();To add items to a C# list, use the Add() method −teams.Add("US"); teams.Add("Canada"); teams.Add("India"); teams.Add("Australia");You can try to run the following code to add items to a list in C# −Exampleusing System; using System.Collections.Generic; public class Demo { public static void Main(string[] args) { var teams = new List(); teams.Add("US"); teams.Add("Canada"); teams.Add("India"); teams.Add("Australia"); Console.WriteLine("Elements..."); foreach (var countries in teams) { Console.WriteLine(countries); } } }
A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following −Syntaxtry { // Protected code } catch (ExceptionName e1) { // Catch block }The code which is prone to exceptions is placed in the try block. When an exception occurs, that exception occurred is handled by catch block associated with it. Every try block should be immediately followed either by ... Read More
To add string values to a list in C#, use the Add() method.Firstly, declare a string list in C# −List list1 = new List();Now add string items −myList.Add("Jack"); myList.Add("Ben"); myList.Add("Eon"); myList.Add("Tim");Let us see the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo { public class Program { public static void Main(String[] args) { List myList = new List(); myList.Add("Jack"); myList.Add("Ben"); myList.Add("Eon"); myList.Add("Tim"); Console.WriteLine(myList.Count); } } }
To add an element to existing jagged array, just set the value of the element with a new value.Let’s say you need to add an element at the following location −a[3][1]Just set the value −a[3][1] = 500;Above, we accessed the first element of the 3rd array in a jagged array.Let us see the complete code −Exampleusing System; namespace Demo { class Program { static void Main(string[] args) { int[][] x = new int[][]{new int[]{10, 20}, new int[]{30, 40}, new int[]{50, 60}, new int[]{ 70, 80 }, new int[]{ 90, 100 ... Read More
A field marked "read-only", can only be set once during the construction of an object. It cannot be changed −Let us see an example.class Employee { readonly int salary; Employee(int salary) { this.salary = salary; } void UpdateSalary() { //salary = 50000; // Compile error } }Above, we have set the salary field as read-only.If you will change it, then a compile-time error will occur. The same is shown in the above example.Let us now see how to check whether an array is read-only or not −Exampleusing ... Read More
Set the string array for the values −string[] names = new string[] {"Jack", "Tom"};Now using foreach array, write the content in the file −using (StreamWriter sw = new StreamWriter("names.txt")) { foreach (string s in names) { sw.WriteLine(s); } }The following is an example showing an array of streams to write text to a file −Exampleusing System; using System.IO; namespace FileApplication { class Program { static void Main(string[] args) { string[] names = new string[] {"Jack", "Tom"}; using (StreamWriter sw = ... Read More
Generics allow you to write a class or method that can work with any data type.Write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type. Generics is a technique that enriches your programs in the following ways −It helps you to maximize code reuse, type safety, and performance.You can create generic collection classes. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. You may use these generic ... Read More
Local VariableLocal variables are declared in methods, constructors, or blocks.Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.Access modifiers cannot be used for local variables.Local variables are visible only within the declared method, constructor, or block.Local variables are implemented at stack level internally.There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.final Local Variablefinal is the only allowed access modifier for local variables.final local variable is not required ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP