Reference:-
http://csharp-video-tutorials.blogspot.de/2013/08/part-28-structs-in-c.html
http://csharp-video-tutorials.blogspot.de/2013/08/part-29-difference-between-classes-and.html
When we run a program in .net, Variable and object are
actually created in the memory and they remain in the memory until your program
executes.
So the physical memory in the Computer is logically divided
by .net run time into called a stack and a heap.
class Class_Customer
{
public int ID { get; set; }
public string Name { get; set; }
public static void Main()
{
int i = 0;
if (i==10)
{
int j = 20;
Class_Customer c = new Class_Customer
{ ID = 1, Name = "Amit" };//In 3.0 New way object initialization
}
}
}
In above program variable j scope is in IF block only when
control goes outside if block j will be destroyed automatically same for other
value type. Where as Reference type
Class_Customer c = new
Class_Customer
Reference variable and Object both are
different things altogether.
Reference variable Stores in the Stack, Run time allocates
one memory block in the heap portion and reference variable will points to this
memory block.
Ref var c will be destroyed when program execution goes outside If
block but Object remain in memory there. It will be destroyed from the heap
when GC runs behind the scene and it sees that Object does not any pointer to
it and GC treats as Garbage and removes the memory block from Heap.
So variable type is lost whenever scope is lost whereas reference
type variable gets destroyed when scope is lost and memory block will be
destroyed by the CLR(GC) when it runs
class Class_Customer
{
public int ID { get; set; }
public string Name { get; set; }
public static void Main()
{
//int i = 0;
//if (i==10)
//{
// int j = 20;
// Class_Customer
c = new Class_Customer { ID = 1, Name = "Amit" };//In 3.0 New way
object initialization
//}
int i = 10;
int j = i;
j++;
Console.WriteLine("Value
of i is"+ i);
Console.WriteLine("Value
of j is" + j);
Class_Customer c = new
Class_Customer { ID = 1, Name = "Amit" };
Console.WriteLine("*****************************");
Console.WriteLine("C
object values before copying");
Console.WriteLine("ID
is"+c.ID+" "+"Name is"+"
"+c.Name);
Console.WriteLine("*****************************");
Class_Customer c1 = c;
c1.ID = 2;
c1.Name = "ANIL";
Console.WriteLine("C
object values after copying into new ref variable and i chnaged in c1. Since
both points to the same meory block so if chnages made than it will ebe
reflected in both . where as in value type , they gets initialezed where they
are declared and have diffrent copy, both haVe points to them selves");
Console.WriteLine("*****************************");
Console.WriteLine("C
object values after copying into new ref variable and i chnaged in c1");
Console.WriteLine("ID
is" + c.ID + " " + "Name is" + "
" + c.Name);
Console.ReadLine();
}
}
struct A { int a; }
class B : A { } // Structure are sealed type so Class or Structure can not
inherit Structure Error 1 becoz
derive from sealed type
A structure cannot act as a Base structure for another
structure or a class.
So the immediate Base
class for class is
Object
and immediate Base class for Structure is ValueType
which inherits from Object
.
1. Why do we want to have class as Abstract?
Abstract – Abstract classes are incomplete classes, i.e., they
will have combination of implemented and unimplemented methods along with data
members, properties, events, delegates and indexers.
The
main idea to have class as abstract is to have only Base class that can be derivable
and that will have basic functionality. As I mentioned above, we can have
unimplemented method in abstract class that gives flexibility to derived
classes to implement as required along with basic functionality. And also, we
can’t instantiate object of abstract class. Abstract class can be with/without abstract methods.
But if methods are declared as abstract in a class, that class also should be declared
as abstract.
Sealed – Sealed classes are classes that are not inheritable.
Methods also can be sealed, that is, those methods declared as sealed can’t be overridable,
i.e., derived classed can’t override those methods. And normal classes can’t
have sealed method. Sealed keyword should be declared with override keyword
in the derived class' method for which base class will have virtual method.
Why do we want to have class and method as
Sealed?
The
reason to have class and method as sealed is to stop the inheritance at one
level.
Where to use sealed?
If
you think that class is not to be inherited in your design, you can use class
as sealed. But sealed class can inherit from
interface and class. If you think virtual method cannot to be inherited in derived class
at one stage, we can declare a method with sealed+override combination.
By
default structures are sealed, that is the reason structures are not
supporting inheritance.
Why do we want to have class and methods as
static?
In
your design, if you feel that a class should have a set of methods which
operate only on arguments that we pass without initiating object of that class,
then we can make that class as static class (Example:System.Math). Merely call those
instance methods by class name.
How this class will be loaded without creating
an object?
If
your code accesses any of static class’s methods, then this is the
responsibility of CLR (Common Language Runtime) to load this class into memory
once which is the lifetime of your application.
Object
of static class can’t be
instantiated. Why? Since static class can’t have instance constructor.Static class supports
inheritance but other classes can’t inherit from static class, i.e., static classes are sealed.
Classes can have static methods but static classes can’t have instance member and
instance methods .if, should be declared with static keyword.
Static class is useful when you implement Singleton
Design pattern.
What does partial modifier indicate?
Partial
key word is to declare the class as partial meant to say it splits the classes
into multiple parts under the same class name in a single namespace.
Why?
So that developers can implement the functionally for the same class parallely.
But
all combined will give one class. Each split class can have instance variable,
instance methods, properties, indexers, events and delegates.
Structures
can’t have modifiers like abstract, sealed, and static whereas classes can have.
Both
structure and class can have partial modifiers.
As
I mentioned earlier, structures can have method declaration but not virtual and sealed methods. Why? Since
those two are essential for inheritance. Anyhow, the structure won’t
support inheritance and declaring methods using those two keywords will throw
compile time errors.
2. Classes can have explicitly parameterless
constructors whereas structures can’t.
3. Member variable initialization is possible in
class whereas in Structures, it is not.
4. It is not possible to declare destructor in
structure but in class it is possible.
5. Process of converting structure type into
object type is called boxing and process of converting object type into
structure type is called unboxing.
Example: int a=10;
Collapse | Copy Code
Object ob = (object) a; //Boxing
a= (int) obj; //Unboxing
6. The “this” keyword gives different meaning in structure
and class. How?
a. In class, “this” keyword is classified as value type of class
type within which it is used like inside instance constructor or instance
method.
b. In structure, “this” keyword is classified as variable type of
structure type within which it is used.
When to Use Structure and Class?
In general, classes can be used when you have
more complex behavior or data. And if you think that these behaviour or data to
be modified after creating an instance of class, then classes are absolute
methods.
Structures can be used
for small data structures. If developer feels that data members of structure
cannot to be modified after creating structure, then having structure will
suit.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace KudVenkatCsharp3
{
class Class_Customer
{
public int ID { get; set; }
public string Name { get; set; }
~Class_Customer()
{
}
public static void Main()
{
//int i = 0;
//if (i==10)
//{
// int j = 20;
// Class_Customer
c = new Class_Customer { ID = 1, Name = "Amit" };//In 3.0 New way
object initialization
//}
int i = 10;
int j = i;
j++;
Console.WriteLine("Value
of i is" + i);
Console.WriteLine("Value
of j is" + j);
Class_Customer c = new
Class_Customer { ID = 1, Name = "Amit" };
Console.WriteLine("*****************************");
Console.WriteLine("C object values before copying");
Console.WriteLine("ID
is" + c.ID + " " + "Name is" + "
" + c.Name);
Console.WriteLine("*****************************");
Class_Customer c1 = c;
c1.ID = 2;
c1.Name = "ANIL";
Console.WriteLine("C
object values after copying into new ref variable and i chnaged in c1. Since
both points to the same meory block so if chnages made than it will ebe
reflected in both . where as in value type , they gets initialezed where they
are declared and have diffrent copy, both haVe points to them selves");
Console.WriteLine("*****************************");
Console.WriteLine("C
object values after copying into new ref variable and i chnaged in c1");
Console.WriteLine("ID
is" + c.ID + " " + "Name is" + "
" + c.Name);
//Classes can have Destructer but structure wont have
Console.ReadLine();
}
}
public struct Struct_Customer
{
public int Id;
public string Name;
Struct_Customer(int i, string str)
{
this.Id = i;
this.Name = str;
}
//
~Struct_Customer() { } //Error 1 Only class types can contain destructors
}
interface IInterface
{
}
// struct Struct_Customer1 : Struct_Customer { } //
Structure can not inherit class or another structure but class can . Only
interface it willError 1 Type 'Class_Customer' in interface list is
not an interface.
struct Struct_Customer1
: IInterface { }
struct Program4
{
//public static void Main()
//{
// Struct_Customer
c = new Struct_Customer();
// c.Id = 12;
// c.Name =
"ait";
//}
}
struct A { int a; }
class B : A { } // Structure are sealed type so Class or Structure can not
inherit Structure Error 1 becoz
derive from sealed type
}
Checl part
No comments:
Post a Comment