Add String Values to a Chash List

karthikeya Boyini
Updated on 21-Jun-2020 14:51:06

17K+ Views

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);       }    } }

Add Items to Existing Jagged Array in C#

Ankith Reddy
Updated on 21-Jun-2020 14:50:24

913 Views

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

Add Read-Only Property in C#

Samual Sam
Updated on 21-Jun-2020 14:47:38

312 Views

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

An Array of Streams in C#

karthikeya Boyini
Updated on 21-Jun-2020 14:44:13

774 Views

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 in C#

Chandu yadav
Updated on 21-Jun-2020 14:43:04

503 Views

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

Final Local Variable in Java

Vikyath Ram
Updated on 21-Jun-2020 14:41:03

3K+ Views

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

Final Static Variables in Java

Vikyath Ram
Updated on 21-Jun-2020 14:39:59

6K+ Views

Final Static VariablesClass variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.There would only be one copy of each class variable per class, regardless of how many objects are created from it.Static variables are normally declared as constants using the final keyword. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.Static variables are stored in the static memory, mostly declared as final and used as either public or private constants.Static variables are created when the program ... Read More

Find Free Disk Space Using Java

Vikyath Ram
Updated on 21-Jun-2020 14:37:34

952 Views

java.io.File class provides following useful methods to figure out the free disk space available.Sr.No.Method & Description1public long getFreeSpace()Returns the number of unallocated bytes in the partition named by this abstract path name.2public long getTotalSpace()Returns the size of the partition named by this abstract pathname.3public long getUsableSpace()Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.Following example showcases the use of above methods.Example Finalimport java.io.File; import java.text.NumberFormat; public class Tester {    public static void main(String[] args) {       NumberFormat numberFormat = NumberFormat.getInstance();       numberFormat.setMaximumFractionDigits(2);     ... Read More

Find Max and Min Values in an Array of Primitives using Java

Arushi
Updated on 21-Jun-2020 14:36:33

548 Views

This example shows how to search the minimum and maximum element in an array by using Collection.max() and Collection.min() methods of Collection class.Exampleimport java.util.Arrays; import java.util.Collections; public class Main {    public static void main(String[] args) {       Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};       int min = (int) Collections.min(Arrays.asList(numbers));       int max = (int) Collections.max(Arrays.asList(numbers));       System.out.println("Min number: " + min);       System.out.println("Max number: " + max);    } }ResultThe above code sample will produce the following result.Min number: 1 Max ... Read More

Flexible Nature of Java Lang Object

Fendadis John
Updated on 21-Jun-2020 14:35:44

196 Views

The java.lang.Object class is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.Class DeclarationFollowing is the declaration for java.lang.Object class −public class ObjectClass constructorsSr.No.Constructor & Description1Object()This is the Single Constructor.Class methodsSr.No.Method & Description1protected Object clone()This method creates and returns a copy of this object.2boolean equals(Object obj)This method indicates whether some other object is "equal to" this one.3protected void finalize()This method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.4Class getClass()This method returns the ... Read More

Advertisements