Month: November 2005
Lazy Instantiation and the Singleton Pattern
{
static Singleton instance=null;
Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}
{
static Singleton instance=null;
Singleton()
{
}
public static Singleton Instance
{
get
{
lock (instance)
{
if (instance==null)
{
instance = new Singleton();
}
return
}
}
}
}
{
static readonly Singleton instance=new Singleton();
// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
static Singleton()
{
}
Singleton()
{
}
public static Singleton Instance
get
{
return instance;
}
}
}
beforefieldinit
. Unfortunately, the C# compiler (as provided in the .NET 1.1 runtime, at least) marks all types which don’t have a static constructor (i.e. a block which looks like a constructor but is marked static) as beforefieldinit
. So to use static members in place of lazy instantiation, the class must have static constructor, otherwise ins laziness is not guaranteed.Blogging in Space
Technical Interview Questions-Part 3
Technical Interview Questions-Part 3
Public Overridable Function OverrideMe() As String
Return "Base Class Override Me"
End Function
Public Function ShadowMe() As String
Return "Base Class Shadow Me"
End Function
End Class
Public Class DerivedShadow
Inherits BaseShadow
Public Overrides Function OverrideMe() As String
Return "Derived Class Override Me"
End Function
Public Shadows Function ShadowMe() As String
Return "Derived Class Shadow Me"
End Function
End Class
Method Hiding:
{
public BaseHide()
{
}
public virtual string OverrideMe()
{
return "Base Class Override Me";
}
public string HideMe()
{
return "Base Class Hide Me";
}
}
public class DerivedHide:BaseHide
{
public DerivedHide()
{
}
public override string OverrideMe()
{
return "Derived Class Override Me";
}
public new string HideMe()
{
return "Derived Class Hide Me";
}
}
Technical Interview Questions – Part 2
Yes
Set its behaviour property to dynamic this will add a new appSetting entry in the config file for the webservice path.
XmlSerializer will refuse to serialize instances of any class that implements IDictionary, e.g. Hash table. Soap Formatter and Binary Formatter do not have this restriction.
A dis-connected record-set is called a DataSet. It is a complete mirror of data-base i.e. we even can keep relationships in it. A data-set can contain multiple tables with relationships among them. User can write the contents of the dataset in an XML file without any effort. A data-set can also be used to move data from one database to another.
What does ‘managed’ mean in the .NET context?
The term ‘managed’ is the cause of much confusion. It is used in various places within .NET, meaning slightly different things.
Managed code: The .NET framework provides several core run-time services to the programs that run within it – for example exception handling and security. For these services to work, the code must provide a minimum level of information to the runtime. Such code is called managed code. All C# and Visual Basic.NET code is managed by default. VS7 C++ code is not managed by default, but the compiler can produce managed code by specifying a command-line switch (/com+).
Managed data: This is data that is allocated and de-allocated by the .NET runtime’s garbage collector. C# and VB.NET data is always managed. VS7 C++ data is unmanaged by default, even when using the /com+ switch, but it can be marked as managed using the __gc keyword.
Managed classes: This is usually referred to in the context of Managed Extensions (ME) for C++. When using ME C++, a class can be marked with the __gc keyword. As the name suggests, this means that the memory for instances of the class is managed by the garbage collector, but it also means more than that. The class becomes a fully paid-up member of the .NET community with the benefits and restrictions that brings. An example of a benefit is proper interop with classes written in other languages – for example, a managed C++ class can inherit from a VB class. An example of a restriction is that a managed class can only inherit from one base class.
What is reflection?
All .NET compilers produce metadata about the types defined in the modules they produce. This metadata is packaged along with the module (modules in turn are packaged together in assemblies), and can be accessed by a mechanism called reflection. The System.Reflection namespace contains classes that can be used to interrogate the types for a module/assembly.
Using reflection to access .NET metadata is very similar to using ITypeLib/ITypeInfo to access type library data in COM, and it is used for similar purposes – e.g. determining data type, their sizes for marshaling data across context/process/machine boundaries.
Reflection can also be used to dynamically invoke methods (see System.Type.InvokeMember), or even create types dynamically at run-time (see System.Reflection.Emit.TypeBuilder). Reflection can also be used for late-binding,r producing generic methods or mosly for dynamically generating classes and modules. Remarkable, isnt it
Technical Interview Questions
a. If I have some common functions to be implemeted in the base class then one should use abstract class as Interface cannot have funcation implementations.
b. Languages like C# and VB.NET only allow single inheritance of classes hence you have only one base class and rest will have to be created as interfaces.
e.g. If I did my class A to inherit from Class B and Class C .. then inheritance can only be done from one of these classes the one will have to created as an interface.
Using Flash for GUI in .Net
1) Create a Web Service
Create a simple "Hello World" Web Service; one that simply returns the string "Hello World" when invoked. If you develop the Service using Visual Studio .NET, the code for "Hello World" is generated automatically. Run and test the Web Service in your web browser. Copy the URL for your Web Service Description or WSDL document i.e. if the URL for your web service is "http://localhost/FlashRemoting/HelloWorld.asmx", then you can access its service description at "http://localhost/FlashRemoting/HelloWorld.asmx?WSDL".
Run Macromedia Flash MX 2004 Professional, and create a new Flash document.
2) Link the Flash Movie to the Web Service
Open the "Web Services" panel by going to Window > Development Panels > Web Services. Click the globe icon on the panel; a small window will appear. Click the "+" Add button on the window, and enter the URL you copied for the WSDL document in step 1. (This will not work if you simply enter the URL for your web service; it MUST be the URL for the WSDL document). When you click the OK button, you will see that the Web Service has been added to the list in the Web Services panel. Expand the "[Your Web Service Name]" Node in the panel and right-click the method you want to call. From the pop-up menu, click "Add Method Call". (An instance of the Web Service Connector component would be added to the stage).
3) Place Flash UI Components on the stage
Go to Window > Development Panels > Components, and drag a "Label" and "Button" control on to the main Flash movie stage.
4) Name all the Component Instances on the stage
Name all 3 component instances on the stage, the Label, the Button, and the WebServiceConnector component from the "Properties" panel.
5) Bind Web Service Result to the UI Component
In order to display the string returned from the web service, you need to bind the result to the Label component you placed on the stage. Go to Window > Development Panels > Component Inspector. Select the Label component on the stage and go to the "Bindings" tab on the "Component Inspector" window. Click the "+" Add Binding button. Select "text : String" from the list and click the OK button. Now that you have specified the UI component property to bind to, you now need to specify the Data Source that would provide the value to display in the Label component. Select the "bound to" item in the Key-Value list on the "Bindings" tab, and then click the Magnifying Glass icon (The "Bound To" window will appear). From the "Component Path" list on the left, select the "WebServiceConnector <[Your Web Service Name]>" node, select "result : String" from the "Schema Location" list on the right, and click the OK button.
6) Add Behavior to Invoke/Trigger the Web Service
To invoke the web service, you need to add a behavior to the button. Go to Window > Development Panels > Behaviors. Select the Button component on the stage and on the "Behaviors" panel, click the "+" Add Behavior button. On the context menu, go to Data > Trigger Data Source (The "Trigger Data Source" window will appear). Select the "[Your Web Service Name]" node and click the OK button.
7) Run the Flash Movie
Press Ctrl+Enter to compile and run the Flash movie. When you click the button, the web service will be invoked and the returned text will appear in the Label component.