Wednesday, 12 April 2017
How to implement singleton design pattern in C#?
In singleton pattern, a class can only have one instance and provides access point to it globally.
Public sealed class Singleton
{
Private static readonly Singleton _instance = new Singleton();
}
Public sealed class Singleton
{
Private static readonly Singleton _instance = new Singleton();
}
What is the difference between directcast and ctype?
DirectCast is used to convert the type of an object that requires the run-time type to be the same as the specified type in DirectCast.
Ctype is used for conversion where the conversion is defined between the expression and the type.
DirectCast
is twice as fast for value types (integers, etc.), but identical for reference types.
Dim MyInt As Integer = 123 Dim MyString1 As String = CType(MyInt, String)Dim MyString2 As String = DirectCast(MyInt, String) ' This will not work
Dim MyObject As Object = "Hello World"Dim MyString1 As String = CType(MyObject, String)Dim MyString2 As String = DirectCast(MyObject, String) ' This will work
A way to check in code if DirectCast will work is by using the TypeOf operator:If TypeOf MyObject Is String Then
DirectCast
|
ctype
|
DirectCast is generally used to cast reference types. | Ctype is generally used to cast value types. |
When you perform DirectCast on arguments that don't match then it will throw InvalidCastException. | Exceptions are not thrown while using ctype. |
If you use DirectCast, you cannot convert object of one type into another. Type of the object at runtime should be same as the type that is specified in DirectCast. Consider the following example: Dim sampleNum as Integer Dim sampleString as String sampleNum = 100 sampleString = DirectCast(sampleNum, String) This code will not work because the runtime type of sampleNum is Integer, which is different from the specified type String. | Ctype can cast object of one type into another if the conversion is valid. Consider the following example: Dim sampleNum as Integer Dim sampleString as String sampleNum = 100 sampleString = CType(sampleNum, String) This code is legal and the Integer 100 is now converted to a string. |
To perform DirectCast between two different classes, the classes should have a relationship between them. | To perform ctype between two different value types, no relationship between them is required. If the conversion is legal then it will be performed. |
Performance of DirectCast is better than ctype. This is because no runtime helper routines of VB.NET are used for casting. | Performance wise, ctype is slow when compared to DirectCast. This is because ctype casting requires execution of runtime helper routines of VB.NET. |
DirectCast is portable across many languages since it is not very specific to VB.NET | Ctype is specific to VB.NET and it is not portable. |
Wrapper classes
A wrapper class is any class which "wraps" or "encapsulates" the functionality of another class or component. These are useful by providing a level of abstraction from the implementation of the underlying class or component;
Not much clear , Right ?
Ok, let me give example from my experience. I have a class for TAX calculation containing method to calculate TAX, and my another class called SALARY calculator is there. Now to calculate salary I want to use TAX calculator class. And my interest is that I will not disclose to other class “How I am calculating TAX”?. Then I will simply wrap my TAX calculator class by SALARY class.
Not much clear , Right ?
Ok, let me give example from my experience. I have a class for TAX calculation containing method to calculate TAX, and my another class called SALARY calculator is there. Now to calculate salary I want to use TAX calculator class. And my interest is that I will not disclose to other class “How I am calculating TAX”?. Then I will simply wrap my TAX calculator class by SALARY class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
namespace BlogProject
{
class Wrapper //Wrapper Class
{
class InnerClass //Inner Class
{
public Int32 ProcessData(int Val)
{
return Val + 1;
}
}
public Int32 CallRapper()
{
Wrapper.InnerClass wpr = new Wrapper.InnerClass(); // Internally call to Wrapper class.
return wpr.ProcessData(100);
}
}
class Program
{
static void Main(string[] args)
{
Wrapper w = new Wrapper();
Console.WriteLine("Data Process by inner class and show by Wrapper Class:-" + w.CallRapper());
Console.ReadLine();
}
}
}
Is C# code is managed or unmanaged code?
C# is managed code because Common language runtime can compile C# code to Intermediate language.
Imp points -
.Net supports two type of coding-
Managed Code-
Imp points -
.Net supports two type of coding-
Managed Code
Unmanaged CodeManaged Code-
The resource, which is with in your application domain is, managed code. The resources that are within domain are faster.
The code, which is developed in .NET framework, is known as managed code. This code is directly executed by CLR with help of managed code execution. Any language that is written in .NET Framework is managed code.
Managed code uses CLR which in turns looks after your applications by managing memory, handling security, allowing cross - language debugging, and so on.
Unmanaged Code-
The code, which is developed outside .NET, Framework is known as unmanaged code.
Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with code of VB, ASP and COM are examples of unmanaged code.
Unmanaged code can be unmanaged source code and unmanaged compile code.
Unmanaged code is executed with help of wrapper classes.
Wrapper classes are of two types: CCW (COM Callable Wrapper) and RCW (Runtime Callable Wrapper).
Wrapper is used to cover difference with the help of CCW and RCW.
Wednesday, 5 April 2017
Strings In C#
Strings
The System.String Class-- String KeyWord used for String Creation
string greeting = "Hello, C#";
A string is a sequence of characters stored in a certain address in memory.
In. NET Framework each character has a serial number from the Unicode
table.
string greeting = "Hello, C#";
Collections in C#
Collections
The types in the Framework for collections can be divided into the following categories: -
Interfaces that define standard collection protocols •
Ready-to-use collection classes (lists, dictionaries, etc.) •
Base classes for writing application-specific collections
Collection Namespace As Follows -
System.Collections Nongeneric collection classes and interfaces
System.Collections.Specialized Strongly typed nongeneric collection classes System.Collections.Generic Generic collection classes and interfaces System.Collections.ObjectModel Proxies and bases for custom collections System.Collections.Concurrent Thread-safe collections
Multithreading??
A thread is defined as the execution path of a program. Each thread defines a unique flow of control.
Thread Life Cycle
The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.
Following are the various states in the life cycle of a thread:
- The Unstarted State: It is the situation when the instance of the thread is created but the Start method is not called.
- The Ready State: It is the situation when the thread is ready to run and waiting CPU cycle.
- The Not Runnable State: A thread is not executable, when:
- Sleep method has been called
- Wait method has been called
- Blocked by I/O operations
- The Dead State: It is the situation when the thread completes execution or is aborted.
System.Threading.Thread
Subscribe to:
Posts (Atom)