Value Type vs Reference Type in C#

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

8K+ 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

227 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

Create a Directory Using C#

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

437 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

6K+ 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

Factory Method to Create Immutable Set in Java SE 9

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

167 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

Define Character Constants in C#

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

282 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

1K+ 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

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

Compare Two Lists for Equality in C#

Samual Sam
Updated on 21-Jun-2020 13:44:24

2K+ Views

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");Now if the following returns different elements, then it would mean the lists are not equal −Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new List < string > ();       list1.Add("P");       list1.Add("Q");       list1.Add("R");       Console.WriteLine("First list..."); ... Read More

Convert Decimal to Octal using C#

Arjun Thakur
Updated on 21-Jun-2020 13:43:30

417 Views

To get the octal equivalent, use a while loop for the decimal value and store the remainder in the array set for octal. Here we have set the remainder by mod 8 in the array.Then divide the number by 8 −while (dec != 0) {    oct[i] = dec % 8;    dec = dec / 8;    i++; }Let us see the complete code.Here, our decimal number is 18 −using System; namespace Demo {    class Program {       static void Main(string[] args) {          int []oct = new int[30];          // decimal          int dec = 18;          int i = 0;          while (dec != 0){             oct[i] = dec % 8;             dec = dec / 8;             i++;          }          for (int j = i - 1; j >= 0; j--)          Console.Write(oct[j]);          Console.ReadKey();       }    } }

Advertisements