Ternary Operator (or) conditional operator In C#

In c# language, ternary operator (?) use to check a conduction, so other name of ternary operator is conditional operator, which work as a if statement. This operator compares two values. It produces a third value that depends on the result of the comparison. At compile-time, the C# compiler translates the ternary expression into branch statements such as brtrue.

public string check(string name, int pass) //Create function

            //Use ternary operator ?

            //condition true then reply “Welcome Arvind”

            //condition falls then reply “Invalid Login or Password”

            return name == “arvind” && pass==123 ? “Welcome Arvind” : “Invalid Login or Password”;            


Polymorphism, Method Hiding and Overriding

Example Class Hierarchy

Let’s assume the following simple class hierarchy with classes A, B and C for the discussions in this text. A is the super- or base class, B is derived from A and C is derived from class B. In some of the easier examples, we will only refer to a part of this class hierarchy.

Continue reading

Some ASP.NET questions

How does output caching work in ASP.NET?

Output caching is a powerful technique that increases request/response throughput by caching the content generated from dynamic pages. Output caching is enabled by default, but output from any given response is not cached unless explicit action is taken to make the response cacheable. Continue reading

What is an interface and what is an abstract class? Please expand by examples of using both. Explain why.

Answer 1

In an interface class, all methods are abstract without implementation whereas in an abstract class, some methods we can define concrete. In interface, no accessibility modifiers are allowed. An abstract class may have accessibility modifiers. Interface and abstract class are basically a set of rules which you have to follow in case you are using them (inheriting them). Continue reading

Finalize and DISPOSE in C#

What is the significance of Finalize method in .NET?

.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (example: Windows API created objects, File, Database connection objects, COM objects, etc.) are outside the scope of .NET Framework. We have to explicitly clean our resources. For these types of objects, .NET Framework providesObject.Finalize method, which can be overridden and clean up code for unmanaged resources can be put in this section? Continue reading

XmlRoot, XmlIgnore and XmlAttribute for XML serialization


XmlIgnore: If you specify XmlIgnore on a field, that field will be ignored when serialized.
XmlAttribute:This Specifies that the XmlSerializer must serialize the class member as an XML attribute.
XmlRoot:Controls XML serialization of the attribute target as an XML root element. You can use the word 
XmlRoot in your code instead of the longer XmlRootAttribute.

XmlElementattribute:  Indicates that a public field or property represents an XML element when the XmlSerializer serializes or deserializes the object that contains it. Set the element name and namespace of the XML element.  By applying an XmlElementAttribute to an array, you instruct   the XmlSerializer to serialize the array as a series of XML  elements, instead of a nested set of elements. Continue reading

C# – Convert XML String to Object

Use the xsd.exe tool which gets installed with the Windows SDK into a directory something similar to: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin

On the first run, you use xsd.exe and you convert your sample XML into a XSD file (XML schema file):

xsd yourfile.xml

This gives you yourfile.xsd, which in a second step, you can convert again using xsd.exe into a C# class:

xsd yourfile.xsd /c

This should give you a file yourfile.cs which will contain a C# class that you can use to deserialize the XML file you’re getting – something like:

XmlSerializer serializer = new XmlSerializer(typeof(msg));
msg resultingMessage = (msg)serializer.Deserialize('yourfile.xml');