C# Object Serialization

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

259 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

417 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

Access Elements from Multi-Dimensional Array in C#

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

320 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();       }    } }

Convert Lower Case to Upper Case using Chash

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

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

Reference (ref) Parameter of an Array Type in C#

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

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

What is a Recursive Method Call in C#

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

411 Views

Recursive method call in C# is called Recursion. Let us see an example to calculate power of a number using recursion.Here, if the power is not equal to 0, then the function call occurs which is eventually recursion −if (p!=0) {    return (n * power(n, p - 1)); }Above, n is the number itself and the power reduces on every iteration as shown below −Exampleusing System; using System.IO; public class Demo {    public static void Main(string[] args) {       int n = 5;       int p = 2;       long res; ... Read More

What is a Sealed Class in C#

karthikeya Boyini
Updated on 21-Jun-2020 12:48:54

810 Views

Sealed class in C# with the sealed keyword cannot be inherited. In the same way, the sealed keyword can be added to the method.When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.Let us see an example of sealed class in C# −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class Program {       static void Main(string[] args) {          Result ob = new ... Read More

What is a Static Constructor in C#

Samual Sam
Updated on 21-Jun-2020 12:46:43

499 Views

A static constructor is a constructor declared using a static modifier. It is the first block of code executed in a class. With that, a static constructor executes only once in the life cycle of class.The following is an example of static constructors in C# −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Difference {    class Demo {       static int val1;       int val2;       static Demo() {          Console.WriteLine("This is Static Constructor");          val1 = 70;       }   ... Read More

What is a Static Class in C#

Chandu yadav
Updated on 21-Jun-2020 12:45:28

577 Views

The C# static class cannot be instantiated and can only have only static members. The static class in C# is sealed and cannot contain instance constructors.The following is an example with static class and static members −Exampleusing System; public static class Demo {    public static float PI = 3.14f;    public static int calc(int n){return n*n;} } class Program {    public static void Main(string[] args) {       Console.WriteLine("PI: "+Demo.PI);       Console.WriteLine("Square: " + Demo.calc(3));    } }Above, the static class is −public static class Demo {    public static float PI ... Read More

Advertisements