Value Type vs Reference Type in C#

Ankith Reddy
Updated on 21-Jun-2020 14:01:19

5K+ Views

Value Type and Reference, both are types in C# −Value TypeValue type variables can be assigned a value directly. They are derived from the class System.ValueType. The value types directly contain data. When you declare an int type, the system allocates memory to store the value.Value Type variables are stored in the stack.Examples are int, char, and float, which stores numbers, alphabets, and floating point numbers, respectively.Reference TypeIt refers to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the ... Read More

Factory method to create Immutable Map in Java SE 9

Vikyath Ram
Updated on 21-Jun-2020 14:01:01

127 Views

With Java 9, new factory methods are added to Map interface to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in concise way.Old way to create collectionsExampleimport java.util.Collections; import java.util.HashMap; import java.util.Map; public class Tester {    public static void main(String []args) {       Map map = new HashMap();       map.put("A", "Apple");       map.put("B", "Boy");       map.put("C", "Cat");       Map readOnlyMap = Collections.unmodifiableMap(map);       System.out.println(readOnlyMap);       try {          readOnlyMap.remove(0); ... Read More

How to create a Directory using C#?

Samual Sam
Updated on 21-Jun-2020 14:00:14

236 Views

To create, move and delete directories in C#, the System.IO.Directory class has methods.Firstly, import the System.IO namespace.Now, use the Director.CreateDirectory() method to create a directory in the specified path −string myDir = @"D:\NEW"; if (!Directory.Exists(myDir)) {    Directory.CreateDirectory(myDir); }In the same way, you can create a sub-directory −string mysubdir = @"C:\NEW\my\"; Directory.CreateDirectory(mysubdir);

Variable Arguments (Varargs) in C#

Arjun Thakur
Updated on 21-Jun-2020 13:59:58

4K+ Views

Use the param keyword to get the variable arguments in C#.Let us see an example to multiply integers. We have used params keyword to accept any number of int values −static int Multiply(params int[] b)The above allows us to find multiplication of numbers with one as well as two int values. The fllowing calls the same function with multiple values −int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10);Let us see the complete code to understand how variable arguments work in C# −Exampleusing System; class Program {    static void Main() {       int mulVal1 = Multiply(5); ... Read More

How to define an array in C#?

karthikeya Boyini
Updated on 21-Jun-2020 13:59:23

95 Views

To define an array in C# −int[] runs = new int[10];Let us now initialize the array in the same line −int[] runs = new int[5] {99, 92, 95};The following is an example displaying how to declare, initialize and display an array −Exampleusing System; namespace Program {    class Demo {       static void Main(string[] args) {          int[] runs = new int[3] {149, 123, 257};          int j;          for (j = 0; j < 3; j++ ) {             Console.WriteLine("Score of Cricketer[{0}] = {1}", j, runs[j]);          }          Console.ReadKey();       }    } }

Factory method to create Immutable Set in Java SE 9

Paul Richard
Updated on 21-Jun-2020 13:58:41

77 Views

With Java 9, new factory methods are added to Set interface to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in concise way.Old way to create collectionsExampleimport java.util.Collections; import java.util.HashSet; import java.util.Set; public class Tester {    public static void main(String []args) {       Set set = new HashSet();       set.add("A");       set.add("B");       set.add("C");       Set readOnlySet = Collections.unmodifiableSet(set);       System.out.println(readOnlySet);       try {          readOnlySet.remove(0);       ... Read More

How is encapsulation implemented in C#?

Samual Sam
Updated on 21-Jun-2020 13:55:05

135 Views

Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers: Public, Private, Protected, Internal, Protected internal, etc.Encapsulation can be understood by taking an example of private access specifier that allows a class to hide its member variables and member functions from other functions and objects.In the following example we have length and width as variables assigned private access specifier −Exampleusing System; namespace RectangleApplication {    class Rectangle {       private double length;       private double width;       ... Read More

How to define character constants in C#?

Chandu yadav
Updated on 21-Jun-2020 13:50:27

190 Views

Character literals are enclosed in single quotes. For example, 'x' and can be stored in a simple variable of char type. A character literal can be a plain character (such as 'x'), an escape sequence (such as '\t'), or a universal character (such as '\u02C0').Let us see an example how to define a character constant in C# −using System; namespace Demo {    class Program {       static void Main(string[] args) {          Console.WriteLine("Welcome!\t");          Console.WriteLine("This is it!");          Console.ReadLine();       }    } }Above, we ... Read More

Externalizable Interface in Java

Vikyath Ram
Updated on 21-Jun-2020 13:49:30

878 Views

Externalization is used whenever we need to customize serialization mechanism. If a class implements an Externalizable interface then, object serialization will be done using writeExternal() method. Whereas at receiver's end when an Externalizable object is a reconstructed instance will be created using no argument constructor and then the readExternal() method is called.If a class implements only Serializable interface object serialization will be done using ObjectoutputStream. At the receiver's end, the serializable object is reconstructed using ObjectInputStream.Below example showcases usage of Externalizable interface.Exampleimport java.io.Externalizable; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; public class ... Read More

How to compare two lists and add the difference to a third list in C#?

Ankith Reddy
Updated on 21-Jun-2020 13:47:41

1K+ Views

First, set the two lists −List OneList < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D");List TwoList < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D");To find the difference between the two list and display the difference elements −IEnumerable < string > list3; list3 = list1.Except(list2); foreach(string value in list3) {    Console.WriteLine(value); }The following is the complete example to compare two lists −Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new ... Read More

Advertisements