.NET Object Serialization – How do you encapsulate the binary serialization?
How do you encapsulate the binary serialization?
public static void SerializeBinary( object o, string file_name)
{
using (FileStream fs = new FileStream(file_name, FileMode.Create)) {
BinaryFormatter fmt = new BinaryFormatter ();
fmt.Serialize(fs, o);
}
}
How do you implement a custom serialization?
CUSTOM SERIALIZATION implementation
public class A: ISerializable
{
private int a;
protected A(SerializationInfo si, StreamingContext sc)
{
this.a = serializationInfo.GetInt32("a");
}
public void ISerializable.GetObjectData(SerializationInfo si, StreamingContext sc)
{
si.AddValue("a", this.a);
}
}
.NET interview: Questions on serialization
How do you encapsulate the binary serialization?
public static void SerializeBinary( object o, string file_name)
{
using (FileStream fs = new FileStream(file_name, FileMode.Create)) {
BinaryFormatter fmt = new BinaryFormatter ();
fmt.Serialize(fs, o);
}
}
How do you implement a custom serialization?
CUSTOM SERIALIZATION implementation
public class A: ISerializable
{
private int a;
protected A(SerializationInfo si, StreamingContext sc)
{
this.a = serializationInfo.GetInt32("a");
}
public void ISerializable.GetObjectData(SerializationInfo si, StreamingContext sc)
{
si.AddValue("a", this.a);
}
}
What do you mean by Explicit Interface Implimentation? What’s Interface Mapping?
A class that implements an interface can explicitly implement a member of that interface. When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface
When class implement interface user mandatory needs to specify interface method body. Such implementation is called as Exlicit
implementation. All the signature of interface API must be compatible with interface API. Dervice class can also define their
own API.
1: interface I1
2: {
3: void someMethod();
4: }
5: Interface I1
6: {
7: void someMethod();
8: }
9:
10: class someClass:I1 I2
11: {
12: void I1.someMethod()
13: {
14: //This is called explicit implementation as i am telling the compiler to implement someMethod of interface I1
15: }
16: void I2.someMethod()
17: {
18: //This is called explicit implementation of someMethod of I2 interface
19: }
20: }
Design Pattern – proxy pattern
Proxy fundamentally is a class functioning as in interface which points towards the actual class which has data. This actual data can be a huge image or an object data which very large and can not be duplicated. So you can create multiple proxies and point towards the huge memory consuming object and perform operations. This avoids duplication of the object and thus saving memory. Proxies are references which points towards the actual object.
Figure ‘Proxy and actual object’ shows how we have created an interface which is implemented by the actual class. So the interface ‘IImageProxy’ forms the proxy and the class with implementation i.e. ‘clsActualImage’ class forms the actual object. You can see in the client code how the interface points towards the actual object.
Figure: - Proxy and actual object
The advantages of using proxy are security and avoiding duplicating objects which are of huge sizes. Rather than shipping the code we can ship the proxy, thus avoiding the need of installing the actual code at the client side. With only the proxy at the client end we ensure more security. Second point is when we have huge objects it can be very memory consuming to move to those large objects in a network or some other domain. So rather than moving those large objects we just move the proxy which leads to better performance.
10 Frequently asked C# questions, Class Questions
- What is the syntax to inherit from a class in C#?
Place a colon and then the name of the base class.
Example: class MyNewClass : MyBaseClass - What is an interface class?
Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. - Why can’t you specify the accessibility modifier for methods inside the interface?
They all must be public, and are therefore public by default. - What’s an abstract class?
A class that cannot be instantiated. An abstract class is a class that must be inherited and have the methods overridden. An abstract class is essentially a blueprint for a class without any implementation. - When do you absolutely have to declare a class as abstract?
1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
2. When at least one of the methods in the class is abstract. - Can you prevent your class from being inherited by another class?
Yes. The keyword “sealed” will prevent the class from being inherited. - What is the difference between a Struct and a Class?
Structs are value-type variables and are thus saved on the stack, additional overhead but faster retrieval. Another difference is that structs cannot inherit. - Can you allow a class to be inherited, but prevent the method from being over-ridden?
Yes. Just leave the class public and make the method sealed. - What’s the difference between an interface and abstract class?
In an interface class, all methods are abstract - there is no implementation. In an abstract class some methods can be concrete. In an interface class, no accessibility modifiers are allowed. An abstract class may have accessibility modifiers. - Can you inherit multiple interfaces?
Yes. .NET does support multiple interfaces.