Archive for the ‘C#’ Category

Understanding Polymorphism in OOP Languages: Abstract Classes vs. Interfaces

February 27, 2016

Found an awesome post in LinkedIn blog. It has been explained very well on this regard. Can’t control sharing to the followers: 🙂

Here you go…..and hope you would love reading!

https://www.linkedin.com/pulse/understanding-polymorphism-oop-languages-abstract-classes-jason-roell

Courtesy to : Jason Roell

 

Advertisements

.NET GAC (Global Assembly Cache) folder location

March 6, 2014

Hope, some of you still think that the default GAC folder is c:\windows\assembly [%windir%\assembly] as such. But there is a BIG “NO”.

Yes, starting with the .NET Framework 4, the default location for the global assembly cache has been changed.

Reference: http://msdn.microsoft.com/en-us/library/yf1d93sz(v=vs.110).aspx

In .NET Framework 4.0, the GAC went through a few changes. The GAC was split into two, one for each CLR. The CLR version used for both .NET Framework 2.0 and .NET Framework 3.5 is CLR 2.0. There was no need in the previous two framework releases to split GAC. The problem of breaking older applications in Net Framework 4.0.

To avoid issues between CLR 2.0 and CLR 4.0, the GAC is now split into private GAC’s for each runtime. The main change is that CLR v2.0 applications now cannot see CLR v4.0 assemblies in the GAC.

.NET 2.0 GAC:

  • c:\windows\assembly – Non-native 32bit and 64bit assemblies.

.NET 4.0 GAC, below are the folders.

  • c:\windows\Microsoft.NET\assembly\GAC_32 – Non-native 32bit assemblies.
  • c:\windows\Microsoft.NET\assembly\GAC_64 – Non-native 64bit assemblies visible only on 64bit Windows.
  • c:\windows\Microsoft.NET\assembly\GAC_MSIL – Non-native MSIL (AnyCPU – 32bit & 64bit) assemblies.

Note:

While trying to load assemblies from the GAC, the CLR first looks at the GAC specific to the platform. If it is unable to find the same, it then moves to the GAC_MSIL.

Happy reading! 🙂

 

Static vs. Singleton Classes – FAQ

February 22, 2014

Just I had gone through an articles over http://java.dzone.com about Static vs. Singleton Classes and related FAQ.Thought of sharing to the wordpress community.

The below excerpt is taken from the link : http://java.dzone.com/articles/singleton-design-pattern-%E2%80%93

FAQs:

Question: Why can’t we use a static class instead of singleton?
Answer:

  • One of the key advantages of singleton over static class is that it can implement interfaces and extend classes while the static class cannot (it can extend classes, but it does not inherit their instance members). If we consider a static class it can only be a nested static class as top level class cannot be a static class. Static means that it belongs to a class it is in and not to any instance. So it cannot be a top level class.
  • Another difference is that static class will have all its member as static only unlike Singleton.
  • Another advantage of Singleton is that it can be lazily loaded whereas static will be initialized whenever it is first loaded.
  • Singleton object stores in Heap but, static object stores in stack.
  • We can clone the object of Singleton but, we can not clone the static class object.
  • Singleton can use the Object Oriented feature of polymorphism but static class cannot.

Question: Can the singleton class be subclassed?
Answer: Frankly speaking singleton is just a design pattern and it can be subclassed. However it is worth to understand the logic or requirement behind subclassing a singleton class as the child class might not inherit the singleton pattern objective by extending the Singleton class. However the subclassing can be prevented by using the final keyword in the class declaration.

Question: Can there be multiple instance of singleton using cloning?
Answer: That was a good catch! What do we do now? To prevent the another instance to be created of the singleton instance we can throw exception from inside the clone() method.

Question: What is the impact if we are creating another instance of singleton using serialization and deserialization?
Answer: When we serialize a class and deserialize it then it creates another instance of the singleton class. Basically as many times as you deserialize the singleton instance it will create multiple instance. Well in this case the best way is to make the singleton as enum. In that way the underlying Java implementation takes care of all the details. If this is not possible then we will need to override the readobject() method to return the same singleton instance.

Question: Which other pattern works with Singleton?
Answer:There are several other pattern like Factory method, builder and prototype pattern which uses Singleton pattern during the implementation.

Question: Which classes in JDK uses singleton pattern?
Answer: java.lang.Runtime : In every Java application there is only one Runtime instance that allows the application to interface with the environment it is running. The getRuntime is equivalent to the getInstance() method of the singleton class.

Enjoy reading.

Courtesy: Mainak Goswami

Misleading exception with .NET DataContractJsonSerializer() – A bug reported to http://connect.microsoft.com/

December 3, 2013
Please find below the complete text from connect.microsoft:
Note: Just copy pasted  as is, so that you may not be finding formatted nicely. 🙂
Description
I was working for serializing C# object into JSON using DataContractJsonSerializer(). Whenever the KnownType in not specified in the base class or passed into the constructor of DataContractJsonSerializer(), I have got the following exception.[Exception]
An unhandled exception of type ‘System.Runtime.Serialization.SerializationException’ occurred in System.Runtime.Serialization.dllAdditional information: Type ‘DerivedClasses.CustomerResponse’ with data contract name ‘CustomerResponse:http://schemas.datacontract.org/2004/07/DerivedClasses’ is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
[/Exception]

Details

Visual Studio/Visual Studio Online/Team Foundation Server/.NET Framework Tooling Version

.NET Framework 4.5.1

What category (if any) best represents this feedback?

Compatibility

Steps to reproduce

1). Have a base class and derived class
2). Don’t mark the base class with [KnownType] attribute
3). Populate derived object with required data
4). Try serializing derived object to JSON using DataContractJsonSerializer(), by passing the typeof(base class)

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

Issues with the exception:The exception what we are getting is as same as for “DataContractSerializer”.

So, this exception message is misleading by saying
1). “Consider using a DataContractResolver”, though DataContractJsonSerializer() is not supporting DataContractResolver.
2). or by adding them to the list of known types passed to “DataContractSerializer”

Expected results

The above two points in “Actual results” section needs to be removed from the exception and to be corrected with specific to “DataContractJsonSerializer”.

Locale

English Connect
Posted by laks_win on 12/2/2013 at 10:49 AM

Hi Team,I have got one more issue on the same Misleading exception with DataContractJsonSerializer() for passing the object which has “Cyclic References”. Actually DataContractJsonSerializer() doesn’t support it, but shows the exception that is specific to DataContractSerializer(). So, please have a check for this issue too.

Thanks and regards
Lakshminarayanan.R

Posted by Microsoft on 11/8/2013 at 2:39 PM

Hi Laks_win.Thank you for the feedback on WCF. This is a good suggestion and we will consider this for a future release.

Thanks,
Erica Mohler
Microsoft Program Manager

Posted by Microsoft on 10/29/2013 at 4:04 AM
Thank you for submitting feedback on Visual Studio and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by laks_win on 10/24/2013 at 9:54 PM
Thanks for the follow up. Just uploaded a newly created sample zip, its for your kind perusal.
Posted by Microsoft on 10/24/2013 at 6:22 AM

Thank you for submitting feedback on Visual Studio and .NET Framework. In order to efficiently investigate and reproduce this issue, we are requesting additional information outlined below.Could you please give us a demo project to demonstrate this issue so that we can conduct further research?

We look forward to hearing from you with this information.

Posted by Microsoft on 10/23/2013 at 10:50 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)

Features in Visual Studio 2012 and ASPNET 4.5” Free E-book

August 4, 2012

There is an E-Book available for download in Telerik’s website. This e-book contains an overview of the features that would help great you as a developer :).

So, what is in this e-book you ask? Well…..

  • JavaScript Intellisense
  • CSS Intellisense
  • ASP.NET and Web API (overview and CRUD operations)
  • Strongly Typed Data Binding
  • Page Inspector (new debugging tool)
  • Etc

Here you go…

Free E-book: Time-Saving VS11 and ASP.NET 4.5 Features You Shouldn’t Miss

LINQ and Dynamic Query – Comparing DateTime value

October 20, 2011

In one of our development, we end up with the situation that needs to check the DateTime value in Dynamic LINQ query. Trying different ways out there and googling a bit, finally got some useful references that helped us to resolve the same

Hope, you would love reading!

 

Example:

TestEntities te = new TestEntities();

string dateString = “12/01/2010”;

 

Used the following overload for “.Where” :

# 1:

IQueryable<T>.Where(Expression<Func<State, bool>> predicate)

var dateTest = te.SiteLogs

.Where(String.Format(“it.TimeStamp >= DATETIME ‘{0}'”, Convert.ToDateTime(dateString).ToString(“yyyy-MM-dd HH:mm”)))

.Select(“it.IPAddress”);

 

# 2:

IQueryable.Where(string predicate, params object[] values)

var dateTest1 = te.SiteLogs

.Where(“it.TimeStamp >= @0”,  Convert.ToDateTime(dateString))

.Select(“it.IPAddress”);

 

Observations:

In # 1,

  • We need to say the “Time” format also, that is compulsory. Otherwise it would throw an error as well.  And again, the syntax with “DATETIME” is wired 🙂
  • The resultant date format should be “yyyy-MM-dd “

 

In # 2,

Its clean and simple. I like this syntax. Here you can add any number of Params like @0, @1 & so on

 

References:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

http://msdn.microsoft.com/en-us/library/bb399176.aspx