C# to JAVA

http://www.25hoursaday.com/CsharpVsJava.html#interfaceMI

http://www.kynosarges.org/JavaCSharp.html#NestedClasses

http://www.harding.edu/fmccown/java_csharp_comparison.html

http://it.toolbox.com/blogs/codesharp/java-for-c-developers-21248

http://www.c-sharpcorner.com/Blogs/12379/switching-to-java-is-self-destructive.aspx

Advertisements

== and Equals() Method in C#

When we create any object there are two parts to the object one is the content and the other is reference to that content.So for example if you create an object as shown in below code:-“.NET interview questions” is the content.“o” is the reference to that content.object o = “.NET Interview questions
When we create any object there are two parts to the object one is the content and the other is reference to that content.
Note:
When you are using string data type it always does content comparison. In other words you either use “.Equals()” or “==” it always do content comparison. 
The rule of thumb is that for nearly all reference types, use Equals when you want to test the equality rather than reference identity. The exception is for strings, comparing strings with == does make things much simpler and more readable but you need to remember that both sides of the operator must be expressions of type string for the comparison to work properly.
So for example if you create an object as shown in below code:-

  1. “.NET interview questions” is the content.
  2. “o” is the reference to that content.

 “==” compares if the object references are same while “.Equals()” compares if the contents are same.

So if you run the below code both “==” and “.Equals()” returns true because content as well as references are same.

object o = ".NET Interview questions";
object o1 = o;
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
Console.ReadLine();
-----------

True
True

Now consider the below code where we have same content but they point towards different instances. So if you run the below code both “==”   will return false and “.Equals()”  will return true.

object o = ".NET Interview questions";
object o1 = new string(".NET Interview questions".ToCharArray());
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
Console.ReadLine();

Now let's see one more example that shows that the Equals() method is an extension method of the string class when you assign a null value to the string variable and using that variable the Equals() method then gets an exception of a null reference so you must be sure that your variable doesn’t have null values when calling the Equals() method. When your one variable contains a null value then you should use that variable as an argument for the Equals() method.

myName.Equals(name);  // Throws NullReference exception if myName is Null.
Null Reference Exception
 
The rule of thumb is that for nearly all reference types, use Equals when you want to test the equality rather than reference identity. The exception is for strings; comparing strings with == does make things much simpler and more readable but you need to remember that both sides of the operator must be expressions of type string for the comparison to work properly.

 

 

 

 

IDisposable and GC

My assumption is that if I implement IDispose on an object, I can explicitly ‘destruct’ it as opposed to waiting for the garbage collector to do it. Is this correct?

The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as DB connections, or open files and streams.  Dispose is not about memory management, it’s about unmanaged resource management Continue reading

Static Readonly variables

Why Static variables?

static variable shared the value of it among all instances of the class. Changes done to static variable are visible across methods. Static variable, that means it exists exactly one place in memory for the duration of the program. Static class variables are like globals. Static variables belong to type, not to its instance. You can change the value of a static variable in any of the method of that class.   Continue reading

Setting Return statement to Null in the finally block.

For example, If x is a local variable, will be effectively set to null anyway when the method is exited and the value of the return value is not null.

The finally statement is executed, but the return value isn’t affected. The execution order is:

  1. Code before return statement is executed
  2. Expression in return statement is evaluated
  3. finally block is executed
  4. Result evaluated in step 2 is returned

Continue reading

Abstract Class Vs Interface

An abstract class is a special kind of class that has no implementation. It cannot be instantiated. Its implementation logic is provided by the classes that derive from it. It can have both abstract as well as non-abstract methods.
It is not compulsory to have only abstract methods in an abstract class. We can also have an abstract class with only non-abstract methods.

Why do we need an Abstract Class? Continue reading

IEnumerable Vs IQueryable

Below lists the differences between them based on their properties :

  IEnumerable  IQueryable
Namespace System.Collections Namespace System.Linq Namespace
Derives from No base interface Derives from IEnumerable
Deferred Execution Supported Supported
Lazy Loading Not Supported Supported
How does it work While querying data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data. Hence does more work and becomes slow. While querying data from database, IQueryable execute select query on server side with all filters. Hence does less work and becomes fast.
Suitable for LINQ to Object and LINQ to XML queries. LINQ to SQL queries.
Custom Query Doesn’t supports. Supports using CreateQuery and Execute methods.
Extension mehtod
parameter
Extension methods supported in IEnumerable takes functional objects. Extension methods supported in IEnumerable takes expression objects i.e. expression tree.
When to use when querying data from in-memory collections like List, Array etc. when querying data from out-memory (like remote database, service) collections.
Best Uses In-memory traversal Paging

Polymorphism: Method hiding and overriding

Creating a method in derived class with same signature as a method in base class is called as method overriding.
Same signature means methods must have same name, same number of arguments and same type of arguments.
Method overriding is possible only in derived classes, but not within the same class.
When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.
To allow the derived class to override a method of the base class, C# provides two options,virtual methods and abstract methods.
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract.

Method Hiding

If a method is not overriding the derived method, it is hiding it in the Derived class.  A hiding method has to be declared using the new keyword. Continue reading

Delegates ( for code Re usability in C#)

Why Delegates?

Delegates give developers the power to make methods that can have behavior injected into them, while retaining the type safety of C#. They lend themselves well to writing highly reusable and DRY code. They are very effective when used as callbacks to various system events.
Deligate is used to hookup our Event to EventHandler method. Delegates are type safe function pointers. Delegates are widely used in events declaration.

Continue reading

By Sriramjithendra Posted in C#.NET