Tuesday, October 13, 2015

Difference between Struct and Class in .Net

Hello All,

As always i am back with some confusion. I am asking very simple question.

"What is the difference between Struct and Class in .NET???"

I was confused because i think both of them provides you approach to create your own type then ?? what can be the difference??

So as a simple answer i found that

Struct :- Value Type
Class :-  Reference Type

Here i am also putting simple code.

using System;

namespace StructDemo
{
    class Program
    {
        static void Main()
        {
            Book objBook = new Book()
            {
                Name = "C Language"
            };

            Console.WriteLine(objBook.Name);
            ChangeBookName(objBook);
            Console.WriteLine(objBook.Name);

            Vehicle objVehicle = new Vehicle()
            {
                Name = "Kawasaki Ninja"
            };

            Console.WriteLine(objVehicle.Name);
            ChangeVehicleName(objVehicle);
            Console.WriteLine(objVehicle.Name);

            Console.ReadLine();
        }

        public static void ChangeBookName(Book objBook)
        {
            objBook.Name = "C++ Language";
        }

        public static void ChangeVehicleName(Vehicle objVehicle)
        {
            objVehicle.Name = "R15";
        }
    }    
}

Now i am going to create simple structure and class.

public struct Book
{
    public string Name { get; set; }
}
public class Vehicle
{
    public string Name { get; set; }
}

After running this code i found following output. Please take a look.

As Struct is Value type value not changed, because it creates local copy.

And as Class is Reference type value changed, because Object Reference is Passed By VALUE.

 For more information about difference between struct and class you can check the following link.

What's the difference between struct and class in .NET?

If you want to download demo please Click Here.

Please put your suggestion and reviews. 

Sunday, June 7, 2015

Object Reference is Passed By VALUE

Hello Guys,

I am back. Don't you think that title looks confusing. We studied in college and schools that in Methods Objects are automatically passed by reference.

And here i am saying In Methods Object Reference is passed by VALUE.

Actually knowingly or unknowingly we are using it. It's our regular way of Programming.

Here i am making simple program please check it out and before checking output downside please i request you to guess the output.

using System;

namespace ObjectReferenceDemo
{
    class Person
    {
        public String Name { get; set; }
    }
}
using System;

namespace ObjectReferenceDemo
{
    class Program
    {
        static void Main()
        {
            Person objPerson = new Person() { Name = "abc" };
            Console.WriteLine("In Main :- {0}", objPerson.Name);
            Console.ReadLine();


            Initialize(objPerson);
            Console.WriteLine("After Initialize Method :- {0}", objPerson.Name);
            Console.ReadLine();

            MakeNull(objPerson);
            Console.WriteLine("After MakeNull Method :- {0}", objPerson.Name);
            Console.ReadLine();
        }

        static void Initialize(Person objPerson)
        {
            objPerson.Name = "xyz";
        }

        static void MakeNull(Person objPerson)
        {
            objPerson = null;
        }
    }
}

Now Please check all the following output.

1) First Readline :-











2) Second ReadLine :-











3) Last ReadLine :-











Wowww... Don't you think that according to JAVA or .NET we studied in college or schools that objects are automatically passed by reference... should throw NullPointerException.
  • When you pass object to methods you can change it's properties. But you can't replace complete object. Don't you think it's really different.
That's Because of "OBJECT REFERENCES ARE PASSED BY VALUE."

For more information about it you can check the following websites that i referred.

Passing Objects By Reference or Value in C#

Java is Pass-by-Value, Dammit!

Is Java “pass-by-reference” or “pass-by-value”?

Here i am also attaching my project for reference.

Download Project

Thank you guyz. Please check it. And waiting for your comments and replies.

Saturday, May 9, 2015

Programming LAWS

Hello Guys,

Here i am listing some funny programming laws, but i think that all of them are saying truth or tells us what is the actual reality.

Please check it out and also waiting for your comments.
  • If debug is the process of removing bugs then,
    programming must be the process of putting it in
  • Any given program, when running, is obsolete
  • Any program will expand to fill any available memory
  • Program complexity grows until it exceeds the capability of the programmer to maintain it
  • Once a software project starts, the schedule until it is completed is a constant
  • A task always takes longer than you expect, even when you take into account Hofstadter’s Law
  • “Don’t worry if it doesn’t work right. If everything did, you’d be out of a job” 
    - Mosher’s Law of Software Engineering
This is the last which i believe.
  • There is always one more BUG
Some if condition that will make you confuse at first look please check it.
  • !(a && b) == (!a || !b)
  • !(a || b) == (!a && !b)
There are lots of other funny laws available you can check it on following like.



This all is just an hour of copy paste...

I started writing blog just to put some funny contents as well as some regular but really strange bugs we are facing everyday.

Please Guys put your reviews, suggestion in comments.