Saturday, May 25, 2013
Friday, October 5, 2012
Monday, July 2, 2012
C# Coding Standards
Here are our C# coding standards, naming conventions, and best practices. |
1. Naming Conventions and Style
do use PascalCasing for class names and method names.
public class ClientActivity
{ public void ClearStatistics()
{ //... }
public void CalculateStatistics()
{ //... }
}
do use camelCasing for method arguments and local variables.
public class UserLog
{ public void Add(LogEvent logEvent)
{ int itemCount = logEvent.Items.Count; // ... }
}
do not use Hungarian notation or any other type identification in identifiers
// Correct int counter; string name; // Avoid int iCounter; string strName; do not use Screaming Caps for constants or readonly variables
// Correct public static const string ShippingType = "DropShip";
// Avoid public static const string SHIPPINGTYPE = "DropShip";
avoid using Abbreviations. Exceptions: abbreviations commonly used as names,
such as Id, Xml, Ftp, Uri
// Correct UserGroup userGroup; Assignment employeeAssignment; // Avoid UserGroup usrGrp; Assignment empAssignment; // Exceptions CustomerId customerId; XmlDocument xmlDocument; FtpHelper ftpHelper; UriPart uriPart; do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase)
HtmlHelper htmlHelper; FtpTransfer ftpTranfer; UIControl uiControl; do not use Underscores in identifiers. Exception: you can prefix private static variables
with an underscore.
// Correct public DateTime clientAppointment;
public TimeSpan timeLeft;
// Avoid public DateTime client_Appointment;
public TimeSpan time_Left;
// Exception private DateTime _registrationDate;
do use predefined type names instead of system type names like Int16, Single, UInt64, etc
// Correct string firstName; int lastIndex; bool isSaved; // Avoid String firstName; Int32 lastIndex; Boolean isSaved; do use implicit type var for local variable declarations. Exception: primitive types (int, string,
double, etc) use predefined names.
var stream = File.Create(path);
var customers = new Dictionary<int?, Customer>();
// Exceptions int index = 100; string timeSheet; bool isCompleted; do use noun or noun phrases to name a class.
public class Employee
{ }
public class BusinessLocation
{ }
public class DocumentCollection
{ }
do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
public interface IShape
{ }
public interface IShapeCollection
{ }
public interface IGroupable
{ }
do name source files according to their main classes. Exception: file names with partial classes
reflect their source or purpose, e.g. designer, generated, etc.
// Located in Task.cs public partial class Task
{ //... }
// Located in Task.generated.cs public partial class Task
{ //... }
do organize namespaces with a clearly defined structure
// Examples namespace Company.Product.Module.SubModule namespace Product.Module.Component namespace Product.Layer.Module.Group do vertically align curly brackets.
// Correct class Program
{ static void Main(string[] args)
{ }
}
do declare all member variables at the top of a class, with static variables at the very top.
// Correct public class Account
{ public static string BankName;
public static decimal Reserves;
public string Number {get; set;}
public DateTime DateOpened {get; set;}
public DateTime DateClosed {get; set;}
public decimal Balance {get; set;}
// Constructor public Account() { // ... }
}
do use singular names for enums. Exception: bit field enums.
// Correct public enum Color
{ Red,
Green,
Blue,
Yellow,
Magenta,
Cyan
}
// Exception [Flags] public enum Dockings
{ None = 0,
Top = 1,
Right = 2,
Bottom = 4,
Left = 8
}
do not explicitly specify a type of an enum or values of enums (except bit fields)
// Don't public enum Direction : long
{ North = 1,
East = 2,
South = 3,
West = 4
}
// Correct public enum Direction
{ North,
East,
South,
West
}
do not suffix enum names with Enum
// Don't public enum CoinEnum
{ Penny,
Nickel,
Dime,
Quarter,
Dollar
}
// Correct public enum Coin
{ Penny,
Nickel,
Dime,
Quarter,
Dollar
}
Wednesday, June 27, 2012
Call web service using javascript
Step 1:Create one website
Step 2:Right click on root directory in Solution explorer and click on "Add Item"
Step 3: Select web Service and click on Add.
Step 4: Create Web Methods
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for HelloWorld
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
//[System.Web.Script.Services.ScriptService]
public class HelloWorld : System.Web.Services.WebService {
public HelloWorld () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string Hello() {
return "Hello World";
}
[WebMethod]
public string Welcome(string name)
{
return "Welcome! "+name;
}
Step 5: Create Web form
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallWebServiceByJS.aspx.cs"
Inherits="CallWebServiceByJS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/HelloWorld.asmx" />
</Services>
</asp:ScriptManager>
<input type="text" name="Name" id="Name" />
<input type="button" name="CallWS" value="Call Web Service through Ajax" />
</form>
</body>
</html>
Step 6: Add ScriptManager in it
Step 7: Add Serivce element in Script Manager
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/HelloWorld.asmx" />
</Services>
</asp:ScriptManager>
Step 8:Open Webservice class "HelloWorld.cs" and uncomment below highlighted line to call web service methods from javascript
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for HelloWorld
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class HelloWorld : System.Web.Services.WebService {
public HelloWorld () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string Hello() {
return "Hello World";
}
[WebMethod]
public string Welcome(string name)
{
return "Welcome! "+name;
}
}
Step 9:Add below script in web form and call web service method as highlighted code
Now we need some client-side functions, button to trigger Web Service request and a text box to provide the input for the Web Service:
SendRequest- this function will send asyncroneus request to the Web ServiceOnComplete- this function will receive result from the Web ServiceOnError- this function will be triggered if an error occures while executing Web ServiceOnTimeOut- this function will be triggered if Web Service will not respondName- text box with the input for the Web ServiceRequestButton- the button that triggersSendRequestfunction
<script language="javascript">
/*Using Script Manager*/
function SendRequest() {
HelloWorld.Welcome(document.getElementById('Name').value, OnComplete, OnError, OnTimeOut);
}
function OnComplete(arg) {
alert(arg);
}
function OnTimeOut(arg) {
alert("timeOut has occured");
}
function OnError(arg) {
alert("error has occured: " + arg._message);
}
</script>
Step 10: Call method on onclick event of button
<input type="button" name="CallWS" value="Call Web Service through Ajax" onclick="return SendRequest()" />
Step 11: Run and check.
Happy Coding!!!