What is a structure in C#?

George John
Updated on 21-Jun-2020 13:03:43

135 Views

A structure in C# is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.C# structures have the following features −Structures can have methods, fields, indexers, properties, operator methods, and events.Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed.Unlike classes, structures cannot inherit other structures or classes.Structures cannot be used as a base for other structures or classes.A structure can implement one ... Read More

What is an interface in C#?

Samual Sam
Updated on 21-Jun-2020 13:02:27

126 Views

Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. It often helps in providing a standard structure that the deriving classes would follow.Let us see how to declare interface in C# with interface members −public interface InterfaceName {    // interface members }The following is an example showing how to use Interface in C# −Exampleusing System.Collections.Generic; using System.Linq; using System.Text; using System; namespace Demo {    public interface ITransactions {       // ... Read More

User-defined Custom Exception in C#

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

338 Views

C# exceptions are represented by classes. The exception classes in C# are mainly directly or indirectly derived from the System.Exception class.You can also define your own exception. User-defined exception classes are derived from the Exception class.The following is an example −Exampleusing System; namespace UserDefinedException {    class TestTemperature {       static void Main(string[] args) {          Temperature temp = new Temperature();          try {             temp.showTemp();          } catch(TempIsZeroException e) {             Console.WriteLine("TempIsZeroException: {0}", e.Message);       ... Read More

C# object serialization

Ankith Reddy
Updated on 21-Jun-2020 12:59:50

144 Views

For object serialization, you need to refer the below code. Here, we have use the BinaryFormatter.Serialize (stream, reference) method to serialize our sample object.We have set a constructor here −public Employee(int id, string name, int salary) {    this.id = id;    this.name = name;    this.salary = salary; }Now set the file stream −FileStream fStream = new FileStream("d:ew.txt", FileMode.OpenOrCreate); BinaryFormatter bFormat = new BinaryFormatter();An object of the Employee class −Employee emp = new Employee(001, "Jim", 30000); bFormat.Serialize(fStream, emp);

Double brace initialization in Java

Paul Richard
Updated on 21-Jun-2020 12:59:12

308 Views

Double braces can be used to create and initialize objects in a single Java expression. See the example below −Exampleimport java.util.ArrayList; import java.util.List; public class Tester{    public static void main(String args[]) {       List list = new ArrayList();       list.add("A");       list.add("B");       list.add("C");       list.add("D");       list.add("E");       list.add("F");       System.out.println(list);       List list1 = new ArrayList() {       {          add("A"); add("B");add("C");          add("D");add("E");add("F");       ... Read More

How to access elements from jagged array in C#?

Samual Sam
Updated on 21-Jun-2020 12:58:11

501 Views

A Jagged array is an array of arrays. To access an element from it, just mention the index for that particular array.Here, we have a jagged array with 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 } };Let’s say you need to access an element from the 3rd array of integers, for that −a[2][1]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 { ... Read More

How to access elements from multi-dimensional array in C#?

Arjun Thakur
Updated on 21-Jun-2020 12:54:49

218 Views

To acess element from the multi-dimensional array, just add the index for the element you want, for example −a[2,1]The above access element from 3rd row and 2nd column ie element 3 as shown below in out [3,4] array −0 0 1 2 2 4 3 6Let us see whatever we discussed and access element from a 2 dimensional array −Exampleusing System; namespace Program {    class Demo {       static void Main(string[] args) {          int[,] a = new int[4, 2] {{0,0}, {1,2}, {2,4}, {3,6} };          int i, j;          for (i = 0; i < 4; i++) {                 for (j = 0; j < 2; j++) {                Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);             }          }          // accessing element          Console.WriteLine(a[2,1]);          Console.ReadKey();       }    } }

How to convert Lower case to Upper Case using C#?

karthikeya Boyini
Updated on 21-Jun-2020 12:53:47

2K+ Views

To convert Lower case to Upper case, use the ToUpper() method in C#.Let’s say your string is −str = "david";To convert the above lowercase string in uppercase, use the ToUpper() method −Console.WriteLine("Converted to UpperCase : {0}", str.ToUpper());The following is the code in C# to convert character case −Exampleusing System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          string str;          str = "david";          Console.WriteLine("LowerCase : {0}", str);          // convert to uppercase          Console.WriteLine("Converted to UpperCase : {0}", str.ToUpper());          Console.ReadLine();       }    } }

User-defined Exceptions in C# with Example

karthikeya Boyini
Updated on 21-Jun-2020 12:52:40

4K+ Views

An exception is a problem that arises during the execution of a program. A C# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.Define your own exception. User-defined exception classes are derived from the Exception class.The following is an example −Exampleusing System; namespace UserDefinedException {    class TestFitness {       static void Main(string[] args) {          Fitness f = new Fitness();          try {             f.showResult();          } catch(FitnessTestFailedException ... Read More

What is a reference/ref parameter of an array type in C#?

Samual Sam
Updated on 21-Jun-2020 12:50:17

833 Views

Declare the reference parameters using the ref keyword. A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters.Declare a ref parameter −public void swap(ref int x, ref int y) {}Declare a ref parameter of array type −static void Display(ref int[] myArr)The following is an example showing how to work with ref parameter of an array type in C# −class TestRef {    static void Display(ref int[] myArr) {       if (myArr == null) {     ... Read More

Advertisements