Skip to main content

Important point To Remember in C# Coding.

C#

1. Are exceptions used to indicate error rather than returning status or error codes?

2. Are all classes and public methods commented with .NET style comments? Note that comments should discuss the "what" of public methods. Discussion of "how" should be in blocks or in-line with the code in question.

3. Are method arguments validated and rejected with an exception if they are invalid?

4. Are Debug. Asserts used to verify assumptions about the functioning of the code? Comments like, "j will be positive" should be rewritten as Asserts.

5. Do classes that should not be instantiated have a private constructor?

6. Are classes declared as value types only infrequently used as method parameters, returned from methods or stored in Collections?

7. Are classes, methods and events that are specific to an assembly marked as internal?

8. Are singletons that may be accessed by multiple threads instantiated correctly?

9. Are methods that must be overridden by derived classes marked as abstract?

10. Are classes that should not be overridden marked as sealed?

11. Is "as" used for possibly incorrect downcasts?

12. Do classes override To String instead of defining a Dump method for outputting the object's state?

13. Are log messages sent to the logging component instead of Console?

14. Are finally blocks used for code that must execute following a try?

15. Is for each used in preference to the for(int i...) construct?

16. Are properties used instead of implementing getter and setter methods?

17. Are read only variables used in preference to properties without setters?

18. Is the override keyword used on all methods that are overridden by derived classes?

19. Are interface classes used in preference to abstract classes?

20. Is code written against an interface rather than an implementing class?

21. Do all objects that represent "real-world" or expensive resources implement the IDisposable pattern?

22. Are all objects that implement IDisposable instantiated in a using block?

23. Is the lock keyword used in preference to the Monitor. Enter construct?

24. Are threads awakened from wait states by events or the Pulse construct, rather than "active" waiting such as Sleep()?

25. If equals is overridden, is it done correctly?

26. If == and != are overridden, so they redirect to Equals?

27. Do all objects that override Equals also provide an overloaded version of GetHashCode that provides the same semantics as Equals? Note that overrides to GetHashCode should take advantage of the object's member variables, and must return an unchanging hash code.

28. Do all exception classes have a constructor that takes a string and and another constructor that takes a string and an exception?

29. Do all exception classes derive from the base Matrix exceptions and fit correctly into the exception hierarchy?

30. Are all classes that will be marshaled or remoted marked with the Serializable attribute?

31. Do all classes marked with the Serializable attribute have a default constructor? This includes Exception and EventArgs classes.

32. Do all classes that explicitly implement ISerializable provide both the required GetObjectData and the implied constructor that takes a SerializationInfo and a StreamingContext?

33. When doing floating point calculations, are all constants doubles rather than integers?

34. Do all delegates have a void return type and avoid using output or ref parameters?

35. Do all delegates send the sender (publisher) as the first argument? This allows the subscriber to tell which publisher fired the event.

36. Are all members of derived EventArg classes read-only? This prevents one subscriber from modifying the EventArgs, which would affect the other subscribers.

37. Are delegates published as events?

38. Is common setup and teardown nUnit code isolated in Setup and Teardown methods that are marked with the appropriate attribute?

39. Do negative nUnit tests use the ExpectedException attribute to indicate that an exception must be thrown?

Comments

Popular posts from this blog

URL Rewritting

http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/ http://msdn.microsoft.com/en-us/library/ms972974.aspx URL Rewriting in ASP.NET Summary: Examines how to perform dynamic URL rewriting with Microsoft ASP.NET. URL rewriting is the process of intercepting an incoming Web request and automatically redirecting it to a different URL. Discusses the various techniques for implementing URL rewriting, and examines real-world scenarios of URL rewriting. (31 printed pages) Download the source code for this article. Contents Introduction Common Uses of URL Rewriting What Happens When a Request Reaches IIS Implementing URL Rewriting Building a URL Rewriting Engine Performing Simple URL Rewriting with the URL Rewriting Engine Creating Truly "Hackable" URLs Conclusion Related Books Introduction Take a moment to look at some of the URLs on your website. Do you find URLs like http://yoursite.com/info/dispEmployeeInfo.aspx?EmpID=459-099&type=summ...

SEND A PDF FILE AS AN ATTACHEMENT OF MAIL!

System.Net.Mail.MailMessage m1 = new System.Net.Mail.MailMessage(); m1.From = "manpreet@gmail.com" m1.Subject = "Test mail "; m1.Body = str.ToString(); m1.IsBodyHtml = true; m1.To.Add("jasdeep@gmail.com"); m1.CC.Add("jasdeep123@gmail.com"); m1.Attachments.Add(new Attachment(strserverpath + @"\pdf\" + PdfFileName)); smtp.Send(m1);

Sql Server Tips

IF WE WANT TO SELECT  TOP N ROWS  FROM A TABLE ,  WE USE THE FETCH NEXT  SELECT    * FROM  table1   ---//First m rows ignore first n rows ORDER BY id OFFSET n ROWS FETCH NEXT m ROWS ONLY if n=0, m=10 then first  10 rows if n=10, m=10 then first  10 rows start from 11th row Row_number()   SELECT  ROW_NUMBER() OVER(order by db.id) Insertion Simultaneouly in temp table with output clause DECLARE @table1 table (   Id int,   name nvarchar(50) ); DECLARE @table2 table (  Id int,  name nvarchar(50) ); INSERT INTO @table2 OUTPUT INSERTED.*   INTO @table1   select top 10 id, name  from FinalTable SELECT * FROM @table2; SELECT * FROM @table1;