C#vb

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C#vb as PDF for free.

More details

  • Words: 3,115
  • Pages: 8
C# and VB.Net Explain the differences between Server-side and Client-side code? Server side code executes on the server.For this to occur page has to be submitted or posted back.Events fired by the controls are executed on the server.Client side code executes in the browser of the client without submitting the page. e.g. In ASP.NET for webcontrols like asp:button the click event of the button is executed on the server hence the event handler for the same in a part of the code-behind (server-side code). Along the server-side code events one can also attach client side events which are executed in the clients browser i.e. javascript events. How does VB.NET/C# achieve polymorphism? Polymorphism is also achieved through interfaces. Like abstract classes, interfaces also describe the methods that a class needs to implement. The difference between abstract classes and interfaces is that abstract classes always act as a base class of the related classes in the class hierarchy. For example, consider a hierarchy-car and truck classes derived from four-wheeler class; the classes two-wheeler and four-wheeler derived from an abstract class vehicle. So, the class 'vehicle' is the base class in the class hierarchy. On the other hand dissimilar classes can implement one interface. For example, there is an interface that compares two objects. This interface can be implemented by the classes like box, person and string, which are unrelated to each other. C# allows multiple interface inheritance. It means that a class can implement more than one interface. The methods declared in an interface are implicitly abstract. If a class implements an interface, it becomes mandatory for the class to override all the methods declared in the interface, otherwise the derived class would become abstract. Can you explain what inheritance is and an example of when you might use it? The savingaccount class has two data members-accno that stores account number, and trans that keeps track of the number of transactions. We can create an object of savingaccount class as shown below. savingaccount s = new savingaccount ( "Amar", 5600.00f ) ; From the constructor of savingaccount class we have called the two-argument constructor of the account class using the base keyword and passed the name and balance to this constructor using which the data member's name and balance are initialised. We can write our own definition of a method that already exists in a base class. This is called method overriding. We have overridden the deposit( ) and withdraw( ) methods in the savingaccount class so that we can make sure that each account maintains a minimum balance of Rs. 500 and the total number of transactions do not exceed 10. From these methods we have called the base class's methods to update the balance using the base keyword. We have also overridden the display( ) method to display additional information, i.e. account number. Working of currentaccount class is more or less similar to that of savingaccount class. Using the derived class's object, if we call a method that is not overridden in the derived class, the base class method gets executed. Using derived class's object we can call base class's methods, but the reverse is not allowed. Unlike C++, C# does not support multiple inheritance. So, in C# every class has exactly one base class.

Now, suppose we declare reference to the base class and store in it the address of instance of derived class as shown below. account a1 = new savingaccount ( "Amar", 5600.00f ) ; account a2 = new currentaccount ( "MyCompany Pvt. Ltd.", 126000.00f) ; Such a situation arises when we have to decide at run-time a method of which class in a class hierarchy should get called. Using a1 and a2, suppose we call the method display( ), ideally the method of derived class should get called. But it is the method of base class that gets called. This is because the compiler considers the type of reference (account in this case) and resolves the method call. So, to call the proper method we must make a small change in our program. We must use the virtual keyword while defining the methods in base class as shown below. public virtual void display( ) { } We must declare the methods as virtual if they are going to be overridden in derived class. To override a virtual method in derived classes we must use the override keyword as given below. public override void display( ) { } Now it is ensured that when we call the methods using upcasted reference, it is the derived class's method that would get called. Actually, when we declare a virtual method, while calling it, the compiler considers the contents of the reference rather than its type. If we don't want to override base class's virtual method, we can declare it with new modifier in derived class. The new modifier indicates that the method is new to this class and is not an override of a base class method. How would you implement inheritance using VB.NET/C#? When we set out to implement a class using inheritance, we must first start with an existing class from which we will derive our new subclass. This existing class, or base class, may be part of the .NET system class library framework, it may be part of some other application or .NET assembly, or we may create it as part of our existing application. Once we have a base class, we can then implement one or more subclasses based on that base class. Each of our subclasses will automatically have all of the methods, properties, and events of that base class ? including the implementation behind each method, property, and event. Our subclass can add new methods, properties, and events of its own - extending the original interface with new functionality. Additionally, a subclass can replace the methods and properties of the base class with its own new implementation - effectively overriding the original behavior and replacing it with new behaviors. Essentially inheritance is a way of merging functionality from an existing class into our new subclass. Inheritance also defines rules for how these methods, properties, and events can be merged. In VB.NET we can use implements keyword for inheritance, while in C# we can use the sign ( :: ) between subclass and baseclass. How is a property designated as read-only? In VB.NET: Private mPropertyName as DataType Public ReadOnly Property PropertyName() As DataType Get Return mPropertyName End Get End Property In C#

Private DataType mPropertyName; public returntype PropertyName { get{ //property implementation goes here return mPropertyName; } // Do not write the set implementation }

What is hiding in CSharp ? Hiding is also called as Shadowing. This is the concept of Overriding the methods. It is a concept used in the Object Oriented Programming. E.g. public class ClassA { public virtual void MethodA() { Trace.WriteLine("ClassA Method"); } } public class ClassB : ClassA { public new void MethodA() { Trace.WriteLine("SubClass ClassB Method"); } } public class TopLevel { static void Main(string[] args) { TextWriter tw = Console.Out; Trace.Listeners.Add(new TextWriterTraceListener(tw)); ClassA obj = new ClassB(); obj.MethodA(); // Outputs “Class A Method" ClassB obj1 = new ClassB(); obj.MethodA(); // Outputs “SubClass ClassB Method” } } What is the difference between an XML "Fragment" and an XML "Document." An XML fragment is an XML document with no single top-level root element. To put it simple it is a part (fragment) of a well-formed xml document. (node) Where as a well-formed xml document must have only one root element. What does it meant to say “the canonical” form of XML? "The purpose of Canonical XML is to define a standard format for an XML document. Canonical XML is a very strict XML syntax, which lets documents in canonical XML be compared directly. Using this strict syntax makes it easier to see whether two XML documents are the same. For example, a section of text in one document might read Black & White, whereas the same section of text might read Black & White in another document, and even in another. If you compare those three documents byte by byte, they'll be different. But if you write them all in canonical XML,

which specifies every aspect of the syntax you can use, these three documents would all have the same version of this text (which would be Black & White) and could be compared without problem. This Comparison is especially critical when xml documents are digitally signed. The digital signal may be interpreted in different way and the document may be rejected.

Why is the XML InfoSet specification different from the Xml DOM? What does the InfoSet attempt to solve? "The XML Information Set (Infoset) defines a data model for XML. The Infoset describes the abstract representation of an XML Document. Infoset is the generalized representation of the XML Document, which is primarily meant to act as a set of definitions used by XML technologies to formally describe what parts of an XML document they operate upon. The Document Object Model (DOM) is one technology for representing an XML Document in memory and to programmatically read, modify and manipulate a xml document. Infoset helps defining generalized standards on how to use XML that is not dependent or tied to a particular XML specification or API. The Infoset tells us what part of XML Document should be considered as significant information. Contrast DTDs versus XSDs. What are their similarities and differences? Which is preferred and why? Document Type Definition (DTD) describes a model or set of rules for an XML document. XML Schema Definition (XSD) also describes the structure of an XML document but XSDs are much more powerful. The disadvantage with the Document Type Definition is it doesn’t support data types beyond the basic 10 primitive types. It cannot properly define the type of data contained by the tag. An Xml Schema provides an Object Oriented approach to defining the format of an xml document. The Xml schema support most basic programming types like integer, byte, string, float etc., We can also define complex types of our own which can be used to define a xml document. Xml Schemas are always preferred over DTDs as a document can be more precisely defined using the XML Schemas because of its rich support for data representation. Speaking of Boolean data types, what's different between C# and C/C++? There's no conversion between 0 and false, as well as any other number and true, like in C/C++. How do you convert a string into an integer in .NET? Int32.Parse(string) Can you declare a C++ type destructor in C# like ~MyClass()? Yes, but what's the point, since it will call Finalize(), and Finalize() has no guarantees when the memory will be cleaned up, plus, it introduces additional load on the garbage collector. What's different about namespace declaration when comparing that to package declaration in Java? No semicolon. What's the difference between const and readonly? The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants as in the following example: public static readonly uint l1 = (uint) DateTime.Now.Ticks;

What does \a character do? On most systems, produces a rather annoying beep. Can you create enumerated data types in C#? Yes. What's different about switch statements in C#? No fall-throughs allowed. What happens when you encounter a continue statement inside the for loop? The code for the rest of the loop is ignored, the control is transferred back to the beginning of the loop. How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods. Will finally block get executed if the exception had not occurred? Yes. What's the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception? A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}. Can multiple catch blocks be executed? No, once the proper catch code fires off, the control is transferred to the finally block (if there are any), and then whatever follows the finally block. Why is it a bad idea to throw your own exceptions? Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own exceptions signifies some design flaws in the project. What's the difference between // comments, /* */ comments and /// comments? Single-line, multi-line and XML documentation comments. How do you generate documentation from the C# file commented properly with a command-line compiler? Compile it with a /doc switch. Can you change the value of a variable while debugging a C# application? Yes, if you are debugging via Visual Studio.NET, just go to Immediate window. What's the implicit name of the parameter that gets passed into the class' set method? Value, and it's datatype depends on whatever variable we're changing. How do you inherit from a class in C#? Place a colon and then the name of the base class. Notice that it's double colon in C++. Does C# support multiple inheritance? No, use interfaces instead.

So how do you retrieve the customized properties of a .NET application from XML .config file? Can you automate this process? Initialize an instance of AppSettingsReader class. Call the GetValue method of AppSettingsReader class, passing in the name of the property and the type expected. Assign the result to the appropriate variable. In Visual Studio yes, use Dynamic Properties for automatic .config creation, storage and retrieval. Why is it not a good idea to insert code into InitializeComponent method when working with Visual Studio? The designer will likely through it away, most of the code inside InitializeComponent is autogenerated. Where do you add an event handler? It's the Attributesproperty, the Add function inside that property. e.g. btnSubmit.Attributes.Add(""onMouseOver"",""someClientCode();"") What are jagged array? First lets us answer the question that what an array is? The dictionary meaning of array is an orderly arrangement or sequential arrangement of elements. In computer science term: An array is a data structure that contains a number of variables, which are accessed through computed indices. The variables contained in an array, also called the elements of the array, are all of the same type, and this type is called the element type of the array. An array has a rank that determines the number of indices associated with each array element. The rank of an array is also referred to as the dimensions of the array. An array with a rank of one is called a single-dimensional array. An array with a rank greater than one is called a multidimensional array. Specific sized multidimensional arrays are often referred to as twodimensional arrays, three-dimensional arrays, and so on. Now let us answer What are jagged arrays? A jagged array is an array whose elements are arrays. The elements of jagged array can be of different dimensions and sizes. A jagged array is sometimes called as “array-of-arrays”. It is called jagged because each of its rows is of different size so the final or graphical representation is not a square. When you create a jagged array you declare the number of rows in your array. Each row will hold an array that will be on any length. Before filling the values in the inner arrays you must declare them. Jagged array declaration in C#: For e.g. : int [] [] myJaggedArray = new int [3][]; Declaration of inner arrays: myJaggedArray[0] = new int[5] ; // First inner array will be of length 5. myJaggedArray[1] = new int[4] ; // Second inner array will be of length 4. myJaggedArray[2] = new int[3] ; // Third inner array will be of length 3.

Now to access third element of second row we write: int value = myJaggedArray[1][2]; Note that while declaring the array the second dimension is not supplied because this you will declare later on in the code. Jagged array are created out of single dimensional arrays so be careful while using them. Don’t confuse it with multi-dimensional arrays because unlike them jagged arrays are not rectangular arrays. For more information on arrays: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfarrayspg.asp What is a delegate, why should you use it and how do you call it ? A delegate is a reference type that refers to a Shared method of a type or to an instance method of an object. Delegate is like a function pointer in C and C++. Pointers are used to store the address of a thing. Delegate lets some other code call your function without needing to know where your function is actually located. All events in .NET actually use delegates in the background to wire up events. Events are really just a modified form of a delegate. It should give you an idea of some different areas in which delegates may be appropriate: • •

• •

They enable callback functionality in multi-tier applications as demonstrated in the examples above. The CacheItemRemoveCallback delegate can be used in ASP.NET to keep cached information up to date. When the cached information is removed for any reason, the associated callback is exercised and could contain a reload of the cached information. Use delegates to facilitate asynchronous processing for methods that do not offer asynchronous behavior. Events use delegates so clients can give the application events to call when the event is fired. Exposing custom events within your applications requires the use of delegates.

How does the XmlSerializer work? XmlSerializer in the .NET Framework is a great tool to convert Xml into runtime objects and vice versa If you define integer variable and a object variable and a structure then how those will be plotted in memory. Integer , structure – System.ValueType -- Allocated memory on stack , infact integer is primitive type recognized and allocated memory by compiler itself . Infact , System.Int32 definition is as follows : [C#] [Serializable] public struct Int32 : IComparable, IFormattable, IConvertible So , it’s a struct by definition , which is the same case with various other value types . Object – Base class , that is by default reference type , so at runtime JIT compiler allocates memory on the “Heap” Data structure .

Reference types are defined as class , derived directly or indirectly by System.ReferenceType