Found 35164 Articles for Programming

Java and multiple inheritance

Arjun Thakur
Updated on 10-Aug-2023 12:01:11

6K+ Views

In Java, we use inheritance to allow the creation of a hierarchical classification of classes and objects. As the name suggests, inheritance is the ability of a class to inherit members of another class. The class whose properties are inherited is called a superclass whereas the class that inherits a superclass is called a subclass. We use the extends keyword to inherit the class. There are several types of inheritance in Java such as single and multilevel. In this article, we will specifically explore the multiple inheritance. Multiple Inheritance in Java In the terminology of object-oriented programming, multiple inheritance is ... Read More

C# program to find Intersection of two lists

Samual Sam
Updated on 23-Jun-2020 15:06:18

2K+ Views

To find intersection of two lists in C#, use the Intersect() method.The following is our list 1.List list1 = new List(); list1.Add(2); list1.Add(3); list1.Add(5); list1.Add(7);The following is our list 2.List list2 = new List(); list2.Add(5); list2.Add(4); list2.Add(6); list2.Add(8);The following is the code to find the intersection of two lists in C#.Example Live Demousing 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(2);          list1.Add(3);          list1.Add(5); ... Read More

Extracting MAC address using C#

karthikeya Boyini
Updated on 23-Jun-2020 15:06:50

3K+ Views

A MAC address of a device is a media access control address. It is a unique identifier assigned to a network.The MAC address technology is used by many technologies such as Ethernet, Bluetooth, Fibre Channel, etc.Here, we will use the following method to check for all the network interfaces on the computer.NetworkInterface.GetAllNetworkInterfacesFor this, the NetworkInterfaceType Enumeration is also used to specify the type of network interfaces.string addr = ""; foreach (NetworkInterface n in NetworkInterface.GetAllNetworkInterfaces()) {    if (n.OperationalStatus == OperationalStatus.Up) {       addr += n.GetPhysicalAddress().ToString();       break;    } } return addr;Above, we have used the ... Read More

Extension Methods in C#

Samual Sam
Updated on 23-Jun-2020 15:07:24

630 Views

Extension methods are static methods, which are called as if they were instance methods on the extended type. With Extension methods, you can add methods to existing types without even creating a new derived type, recompiling, or modifying the original type.The following is the extension method we have created.public static int myExtensionMethod(this string str) {    return Int32.Parse(str); }Let us see an example wherein we have used extension method.Example Live Demousing System; using System.Text; namespace Program {    public static class Demo {       public static int myExtensionMethod(this string str) {          return Int32.Parse(str);     ... Read More

How do we use a break statement in while loop in C#?

karthikeya Boyini
Updated on 23-Jun-2020 15:08:57

153 Views

The break statement terminates the loop and transfers execution to the statement immediately following the loop.When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.Let us see an example to learn how to work with break statement in while loop. The following code snippet terminates the loop using break statement.if (a > 15) {    break; }The following is the complete code.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          /* local ... Read More

How do we use a #line directive in C#?

Samual Sam
Updated on 30-Jul-2019 22:30:23

110 Views

It lets you modify the compiler's line number and (optionally) the file name output for errors and warnings. Let us see some examples. #line 100 "demo" int a; // CS0168 on line 100 int b; // CS0168 on line 101 int c; // CS0168 on line 102 As shown above the example reports three warnings associated with line numbers. The #line 100 directive forces the line number to be 100 and until the next #line directive, the filename will be reported as "demo”. Let’s see ... Read More

How do you use ‘foreach’ statement for accessing array elements in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:39:01

100 Views

To access Array elements in a foreach statement, use the numeric index.Let’s say the following is our code.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int [] n = new int[10]; /* n is an array of 10 integers */          /* initialize elements of array n */          for ( int i = 0; i < 10; i++ ) {             n[i] = i + 100;          }       ... Read More

How do you declare jagged arrays in C#

Samual Sam
Updated on 23-Jun-2020 14:39:50

147 Views

A Jagged array is an array of arrays. You can declare a jagged array named scores of type int as.int [][] scores;Let us now see an example to learn how to declare and work with jagged arrays in C#.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          /* a jagged array of 5 array of integers*/          int[][] a = new int[][]{new int[]{0, 0}, new int[]{1, 2}, new int[]{2, 4}, new int[]{ 3, 6 }, new          int[]{ 4, 8 } ... Read More

How do I sort a two-dimensional array in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:41:25

2K+ Views

To sort a two-dimensional array in C#, in a nested for loop, add another for loop to check the following condition.Examplefor (int k = 0; k < j; k++) {    if (arr[i, k] > arr[i, k + 1]) {       int myTemp = arr[i, k];       arr[i, k] = arr[i, k + 1];       arr[i, k + 1] = myTemp;    } }Till the outer loop loops through, use the GetLength() method as shown below. This is done to sort the array.Examplefor (int i = 0; i < arr.GetLength(0); i++) {    for ... Read More

How do we use nested if statements in C#?

Samual Sam
Updated on 23-Jun-2020 14:42:33

98 Views

Use one if or else if statement inside another if or else if statement(s). The syntax for a nested if statement is as follows −if( boolean_expression 1) {    /* Executes when the boolean expression 1 is true */    if(boolean_expression 2) {       /* Executes when the boolean expression 2 is true */    } }The following is an example showing the usage of nested if statements in C#. Here, we have two if statement that checks two conditions.if (a == 5) {    /* if condition is true then check the following */    if (b ... Read More

Advertisements