![]() |
Visual Studio 2010 – Start Page |
![]() |
ASP.NET MVC 2 Web Application |
![]() |
MVC Home Controller Page |
![]() |
MVC 2 Application Home Page |
Practise makes perfect
![]() |
Visual Studio 2010 – Start Page |
![]() |
ASP.NET MVC 2 Web Application |
![]() |
MVC Home Controller Page |
![]() |
MVC 2 Application Home Page |
Example below:
For example:
C#.NET Coding Guidelines
Naming Conventions
1. Prefix member variables with the underscore ‘_’.
2. Do not prefix member variables with “this”.
3. Use camelCasing for member variables.
4. Use camelCasing for parameters
5. Use camelCasing for local variables
6. Use PascalCasing for function, property, event, and class names.
7. Prefix interfaces names with “I”
When platform invoke calls an unmanaged function, it performs the following sequence of actions:
Note Locating and loading the DLL, and locating the address of the function in memory occur only on the first call to the function.
Platform Invoke example below :
using System.Runtime.InteropServices;
public class Win32 {
[DllImport(“user32.dll”, CharSet=CharSet.Auto)]
public static extern int MessageBox(int hWnd, String text,
String caption, uint type);
}
public class HelloWorld {
public static void Main() {
Win32.MessageBox(0, “Hello World”, “Platform Invoke Sample”, 0);
}
}
Consider the below example.
public class Class1 { public string GetClass() { return "This is Class1"; } } public class Parent2 { public string GetClass() { return "This is Class2"; } } public class SubClass: Class1, Class2 { public string GetCorrectClass() { return this.GetClass(); } }
Here there are two Parent class Class1 and Class2. This is inherited by Child class SubClass.
In GetCorrectClass method, child class calls the parent class method GetClass(). In such case which method will be executed?
It is very difficult for CLR to identify which method to call. It shows that multiple inheritance will create ambiguity to oops concept. In order to avoid this ambiguity we do not have multiple class inheritance and we have multiple interface implementations in C#.
Sample Code :
string str = “santosh”;
CharEnumerator chs = str.GetEnumerator();
while(chs.MoveNext())
{
Response.Write(chs.Current);
}
No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.
You can send a comma-separated list of arguments of the type specified in the parameter declaration, or an array of arguments of the specified type. You also can send no arguments
Example :
public static void UseParams(params int[] list)
{
for (int i = 0; i < list.Length; i++)
{
Console.Write(list[i] + ” “);
}
Console.WriteLine();
}
We can call above method
Way 1 :
// You can send a comma-separated list of arguments of the specified type.
UseParams(1, 2, 3, 4);
Way 2 :
// An array argument can be passed, as long as the array
// type matches the parameter type of the method being called.
int[] myIntArray = { 5, 6, 7, 8, 9 };
UseParams(myIntArray);
Way 3:
UseParams();
Various class modifiers are listed below :
The new modifier is permitted on nested classes. It specifies that the class hides an inherited member by the same name. It is a compile-time error for the new modifier to appear on a class declaration that is not a nested class declaration.
An abstract class cannot be sealed.
Singleton Design pattern ensures a class has only one instance and provide a global point of access to it.
Code Snippet showing a simple Singleton Design pattern :
///
public class Singleton
{
// Define a static class variable
private static Singleton _instance;
// Have a private Constructor
private Singleton()
{
}
public static Singleton Instance()
{
// Uses lazy initialization.
// Note: this is not thread safe.
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}
Above implementation has below advantages:
• Singleton provides lazy instantiation i.e the instantiation is not performed until an object asks for an instance. Lazy instantiation avoids instantiating unnecessary singletons when the application starts.
Above implementation has below disadvantages:
•Above implementation of Singleton class is not safe for multithreaded environments. If separate threads of execution enter the Instance property method at the same time, more that one instance of the Singleton object may be created.
Below are two approaches solve this problem.
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
private Singleton(){}
public static Singleton Instance
{
get { return instance; }
}
}
The class is marked sealed to prevent derivation, which could add instances. The variable is marked readonly, which means that it can be assigned only during static initialization (which is shown here) or in a class constructor.
Code Snippet showing a Thread Safe Singleton Design pattern :
///
public class Singleton
{
// Define a static class variable
private static Singleton _instance;
private static object syncRoot = new Object();
// Have a private Constructor
private Singleton()
{
}
public static Singleton Instance()
{
// Uses lazy initialization.
// Note: this is thread safe.
lock (syncRoot)
{
if (_instance == null)
_instance = new Singleton();
}
return _instance;
}
}