Chandu yadav

Chandu yadav

810 Articles Published

Articles by Chandu yadav

Page 9 of 81

C# Linq LastorDefault Method

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 2K+ Views

Use the LastorDefault() method to return the last element of a sequence or a default value if element isn’t there.The following is our empty list.List val = new List { };Now the following will not be able to display the last element since the list is empty. Therefore, the default would get display and error won’t be shown.val.AsQueryable().LastOrDefault();The following is the code.Exampleusing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List val = new List { };       double d = val.AsQueryable().LastOrDefault();       Console.WriteLine("Default Value = "+d);   ...

Read More

How to prevent Reflection to break a Singleton Class Pattern?

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 971 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using reflection, we can still create multiple instance of a class by modifying the constructor scope. See the example below −Example - Breaking Singletonimport java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Tester {    public static void main(String[] args) throws    InstantiationException, IllegalAccessException,    IllegalArgumentException, InvocationTargetException{       A a = A.getInstance();       ...

Read More

C# Program to convert a Byte value to an Int32 value

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 8K+ Views

To convert a Byte value to an Int32 value, use the Convert.ToInt32() method.Int32 represents a 32-bit signed integer.Let’s say the following is our Byte value.byte val = Byte.MaxValue;;Now to convert it to Int32.int intVal = Convert.ToInt32(val);Let us see the complete example.Exampleusing System; public class Demo {    public static void Main() {       byte val = Byte.MaxValue;;       int intVal = Convert.ToInt32(val);       Console.WriteLine("Converted byte {0} to Int32 {1} value ", val, intVal);    } }OutputConverted byte 255 to Int32 255 value

Read More

Display the package name of a class in Java

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 6K+ Views

The package for a class can be obtained using the java.lang.Class.getPackage() method with the help of the class loader of the class. If there is no package object created by the class loader of the class, then null is returned.A program that demonstrates this is given as follows −Exampleimport java.util.Date; public class Main {    public static void main(String[] args) {       Date d = new Date();       Package p = d.getClass().getPackage();       String pName = p.getName();       System.out.println("The package name is: " + pName);    } }OutputThe package name is: java.utilNow ...

Read More

C# Linq Contains Method

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 3K+ Views

To check for an element in a string, use the Contains() method.The following is our string array.string[] arr = { "Java", "C++", "Python"};Now, use Contains() method to find a specific string in the string array.arr.AsQueryable().Contains(str);Let us see the complete example.Exampleusing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       string[] arr = { "Java", "C++", "Python"};       string str = "Python";       bool res = arr.AsQueryable().Contains(str);       Console.WriteLine("Array has Python? "+res);    } }OutputArray has Python? True

Read More

Implement Runnable vs Extend Thread in Java

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 2K+ Views

We can create Thread by either by implementing a runnable interface or by extending Thread class. Below are the detailed steps of using both ways to create Thread.Create a Thread by Implementing a Runnable InterfaceIf your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface. You will need to follow three basic steps −Step 1As a first step, you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. ...

Read More

instanceof operator vs isInstance method in java

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 534 Views

isInstance method is equivalent to instanceof operator. The method is used in case of objects are created at runtime using reflection. General practice says if type is to be checked at runtime then use isInstance method otherwise instanceof operator can be used. See the example below −Examplepublic class Tester{    public static void main(String[] args) throws ClassNotFoundException {       Integer i = new Integer(10);       System.out.println(usingInstanceOf(i));       System.out.println(usingIsInstance(i));    }    public static String usingInstanceOf(Object i){       if(i instanceof String){          return "String";       }   ...

Read More

C# Linq Except Method

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 3K+ Views

Get the difference between two arrays using the Except() method.The following are the two arrays.int[] arr = { 9, 12, 15, 20, 35, 40, 55, 67, 88, 92 }; int[] arr2 = { 20, 35 };To get the difference, use Except() method that returns the first list, except the elements in the second list.arr.AsQueryable().Except(arr2);The following is the entire example.Exampleusing System; using System.Linq; using System.Collections.Generic; class Program {    static void Main() {       int[] arr = { 5, 10, 15, 20, 35, 40 };       int[] except = { 20, 35 };       Console.WriteLine("Initial ...

Read More

Jagged Array in Java

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 5K+ Views

Jagged array is a multidimensional array where member arrays are of different size. For example, we can create a 2D array where first array is of 3 elements, and is of 4 elements. Following is the example demonstrating the concept of jagged array.Examplepublic class Tester {    public static void main(String[] args){       int[][] twoDimenArray = new int[2][];       //first row has 3 columns       twoDimenArray[0] = new int[3];       //second row has 4 columns       twoDimenArray[1] = new int[4];       int counter = 0; ...

Read More

Convert.ChangeType Method in C#

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 3K+ Views

The ChangeType() method returns an object of the specified type and whose value is equivalent to the specified object.Let’s say we have a double type.double val = -3.456Now, use the ChangeType method to change the type to integer.num = (int)Convert.ChangeType(val, TypeCode.Int32);Let us see the complete example.Exampleusing System; public class Demo {    public static void Main() {       double val = -3.456;       int num = (int)Convert.ChangeType(val, TypeCode.Int32);       Console.WriteLine("{0} converted to an Int32: {1}", val, num);    } }Output-3.456 converted to an Int32: -3

Read More
Showing 81–90 of 810 articles
« Prev 1 7 8 9 10 11 81 Next »
Advertisements