Monday, September 28, 2015

Class Modifiers and their uses in C# .Net



This table is for showing relationship between a base class and child class
In this table I want to show the accessibility among the various types of classes and interface. From this table you also can know what type of class can be initiated. This is one example that helps you to understand the table.
If you use Abstract class as base class and take a general class as child and want to inherit, this will possible …here you can see..
Base: Abstract (ASadi)
Child: Interface (Sadi)
Accessibility: Yes
abstract class Sadi : ASadi
{
#region ASadi Members
public int getint() {
throw new Exception(“The method or operation is not implemented.”); }
#endregion
}
If you execute this code, it executes properly
From the first column of the table we can say that general class inherites both general and abstract class and also implements an interface. These classes cannot inherites sealed as well as static class. Only General and Sealed classes can be initiated.
From the table and above discussion we can conclude that,
Abstract Class
A class defined as abstract is used as a base class. Such a class is used for the purpose of inheritance only i.e. other classes are derived from this class. We cannot create an object of an abstract class. An abstract class may contain methods and properties. The classes derived from the abstract class inherit these methods and properties. The abstract class may also contain abstract methods and properties. Abstract method and properties do not have any functionality. The derived class defines their full functionality.
Here is an example of an abstract class:
abstract class MyAbstract { public abstract void AbMethod(); }
Sealed Class
Classes can be declared as sealed. This is accomplished by putting the sealed keyword before the keyword class in the class definition. For example:
public sealed class classSealed { // Class members here. public string ID; public double Price; }
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster. Sealing a class means one cannot derive from it. Sealing a method means one cannot override it. In C# structs are implicitly sealed; therefore, they cannot be inherited. If we try to inherit from a sealed class in another class we will get compile time error about Inconsistent accessibility (code is shown in following code listing). In C# a method cannot be declared as sealed. However when we override a method in a derived class, we can declare the overridden method as sealed as shown below. By declaring it as sealed, we can avoid further overriding of this method.
Static Class
Static classes are classes that contain only static members. Following is an example of static class. public static MyClass { …..}
A class can be declared static, which indicates that it contains only static members. It is not possible to use the new keyword to create instances of a static class. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace that contains the class is loaded.
Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.
Following are the main features of a static class:
·         They only contain static members.
·         They cannot be instantiated.
·         They are sealed.
·         They cannot contain Instance Constructors (C# Programming Guide).
Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.
The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class exceptObject. Static classes cannot contain an instance constructor; however, they can have a static constructor. For more information, see Static Constructors (C# Programming Guide).


File Upload Control Strange Behavior!!!

This is a common problem for fileupload,
In IE this contol returns whole path of the file, while in Mozzila it takes only file name.
For example,
if you choose C:\test.txt in this control, it returns C:\test.txt in IE and test.txt in mozzila
try following code,
<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">
  <title>Untitled Page</title>
 
  <script type="text/C#" runat="server">
 
  protected void Page_Load(object sender, EventArgs e)
  {
  Response.Write(Request.Form["fileUpload"]);
  }
 
  </script>
 
</head>
<body>
  <form id="form1" runat="server">
  <input type="file" id="fileUpload" name="fileUpload" />
  <asp:Button ID="chk" runat="server" Text="PostBack" />
  </form>
</body>
</html>


Go Back in From

Back




.NET Framework (C#) Versions History

Version Date .NET Framework Visual Studio
C# 1.0 January 2002 .NET Framework 1.0 Visual Studio .NET 2002
C# 1.2 April 2003 .NET Framework 1.1 Visual Studio .NET 2003
C# 2.0 November 2005 .NET Framework 2.0 Visual Studio 2005
C# 3.0 November 2007 .NET Framework 2.0
.NET Framework 3.0
.NET Framework 3.5
Visual Studio 2008
Visual Studio 2010
C# 4.0 April 2010 .NET Framework 4 Visual Studio 2010
C# 5.0 August 2012 .NET Framework 4.5 Visual Studio 2012
Visual Studio 2013
C# 6.0 July 2015 .NET Framework 4.6 Visual Studio 2015

Sunday, September 27, 2015

All about C#

Summary of versions
C# 2.0
  • Generics
  • Partial types
  • Anonymous methods
  • Iterators
  • Nullable types
  • Getter/setter separate accessibility
  • Method group conversions (delegates)
  • Co- and Contra-variance for delegates
  • Static classes
  • Delegate inference
C# 3.0
  • Implicitly typed local variables
  • Object and collection initializers
  • Auto-Implemented properties
  • Anonymous types
  • Extension methods
  • Query expressions
  • Lambda expressions
  • Expression trees
  • Partial methods
C# 4.0
  • Dynamic binding
  • Named and optional arguments
  • Generic co- and contravariance
  • Embedded interop types ("NoPIA")
C# 5.0
  • Asynchronous methods
  • Caller info attributes
C# 6.0
  • Compiler-as-a-service (Roslyn)
  • Import of static type members into namespace
  • Exception filters
  • Await in catch/finally blocks
  • Auto property initializers
  • Default values for getter-only properties
  • Expression-bodied members
  • Null propagator (Succinct null checking)
  • String Interpolation
  • nameof operator
  • Dictionary initializer
C# 7.0 Proposals
  • Declaration expressions
  • Parameter arrays for IEnumerable interfaces

LINQ To SQL Vs. Entity Framework


Following are some comparison between LINQ to SQL and entity framework

CategoryLINQ to SQLEntity Framework
Model
domain model

conceptual data model
Databases Supportedas the name indicates: SQL server only
variety of databases

Data Sources

tables only

tables, replication, reporting Services, BI and etc
Complexitysimple to usecomplex to use
Development Timerapid developmentslower development but more capabilities
Mappingclass to single tableclass to multiple tables
Inheritancehard to applysimple to apply
File Typesdbml files onlyedmx files
after compilation generate 3 xml files to represent the schema:
csdl, msl and ssdl
There are more differences between the frameworks but these are the 
most important.

LINQ: Single vs. First


I’ve witnessed and been involved in several discussions around the correctness or usefulness of the Single method in the LINQ API.
The most common argument is that you are querying for the first element on the result set and an exception will be thrown if there’s more than one element. The First method should be used instead, because it doesn’t throw if the result set has more than one item.
Although the documentation for Single states that it returns a single, specific element of a sequence of values, it actually returns THE single, specific element of a sequence of ONE value. When you use the Single method in your code you are asserting that your query will result in a scalar result instead of a result set of arbitrary length.
On the other hand, the documentation for First states that it returns the first element of a sequence of arbitrary length.
Imagine you want to catch a taxi. You go the the taxi line and catch the FIRST one, no matter how many are there.
On the other hand, if you go the the parking lot to get your car, you want the SINGLE one specific car that’s yours. If your “query” “returns” more than one car, it’s an exception. Either because it “returned” not only your car or you happen to have more than one car in that parking lot. In either case, you can only drive one car at once and you’ll need to refine your “query”.

What is the difference between a.Equals(b) and a == b?


For value types: “==” and Equals() works same way : Compare two objects by VALUE
Example:

    int i = 5;
    int k= 5;
    Here “==” and Equals() is as:
    i == k >> True
    i.Equals(k) >> True

 For reference types : both works differently :
“==” compares reference – returns true if and only if both references point to the SAME object while
"Equals" method compares object by VALUE and it will return true if the references refers object which are equivalent
Example:

    StringBuilder objSb1 = new StringBuilder("Elias");
    StringBuilder objSb2 = new StringBuilder("Elias");

    Here “==” and Equals() is as:
    objSb1 == objSb2 >> False
    objSb1.Equals(objSb2) >> True

But now look at following issue:

    string s1 = "Elias";
    string s2 = "Elias";

    Here “==” and Equals() is as:
    In above case the results will be,
    s1 == s2 >> True
    s1.Equals(s2) >> True

String is actually reference type: its a sequence of “Char” and its also immutable but as you saw above, it will behave like Value Types in this case.
Exceptions are always there ;)

Now another interesting case:
    int i = 0;
    byte b = 0;

    Here “==” and Equals() is as:
    i == b >> True
    i.Equals(b) >> False
So, it means Equals method compare not only value but it compares TYPE also in Value Types.

Recommendation:
For value types: use “==”
For reference types: use Equals method.