Both Classes
and Structs are constructs in C#
which are used to create custom complex types by encapsulating a set of basic
types and logic. Though they are used to create custom types there are some
underlying differences between the two, the following are some of the
differences between classes and structs.
Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts
Wednesday, May 29, 2013
Tuesday, May 28, 2013
OUT vs REF Parameters
OUT
and REF parameters are almost
similar, both are used to return additional values from a function to the code
which is calling the function. However there are some difference between the two,
the following are the some of the key differences between the OUT and REF
parameters.
Out parameters are declared by adding the keyword out in front of the parameter.
Out parameters are declared by adding the keyword out in front of the parameter.
REF Parameter
REF parameters are
parameters which are passed between the calling function and the function being
called, we need to declare and initialize the parameters in the calling method,
their values will be re-assigned in the actual function and returned back to
the calling function.
REF parameters are declared similar to the normal parameters by adding a ref keyword in front of the parameter as follows.
REF parameters are declared similar to the normal parameters by adding a ref keyword in front of the parameter as follows.
OUT Parameter
OUT parameters are
parameters which are passed back from a function to the calling function or
method, it is good enough to declare the parameters in the calling method they
need not be initialized in the calling class, instead their values will be
assigned in the function which is returning the out parameters.
OUT parameters are declared similar to the normal parameters by adding an out keyword in front of the parameter as follows.
OUT parameters are declared similar to the normal parameters by adding an out keyword in front of the parameter as follows.
Constants
As the name suggests Constants are a type of class members whose
value is defined at the time of writing the program and remain constant forever,
the values of constants cannot be changed at runtime, the values of a constant defined
in a class will remain same for every instance that is created from the class.
Constants are declared using the const keyword as follows.
Constants are declared using the const keyword as follows.
Monday, May 27, 2013
Anonymous Types
Anonymous
types allow us to define variables and classes dynamically
at runtime without having to declare them beforehand. The following example creates
a string variable MyName dynamically at runtime without any specific
declaration.
var MyName = "Test
Name";
Console.WriteLine(MyName.GetType());
When this code executes the output will be as follows
Nested Classes
As the
name suggests, nested classes are classes which are defined within an outer
parent class. The following example creates a nested class called NestedClass which
is encapsulated in the outer class called OuterClass.
Destructors
Destructors are used to free unused
resources and release memory used by the resources. Destruction of Managed
objects are handled by the Garbage collector in the .Net Framework, we can use
the destructor to release any unmanaged resources like Network connectivity,
database connections etc and release the resources.
The signature of the destructor is similar to a constructor, it shares the same name of the class, the only difference is that we add a ~ symbol in front of the method name to denote that this is a destructor.
The following example defines a destructor for the CEmployee class
The signature of the destructor is similar to a constructor, it shares the same name of the class, the only difference is that we add a ~ symbol in front of the method name to denote that this is a destructor.
The following example defines a destructor for the CEmployee class
Constructors
Constructors are methods of a class which get executed
automatically when an instance of a class is created. The constructor method
name should be same as the class name.
The following example defines a simple constructor for
the class CEmployee
Saturday, May 25, 2013
What is an Object?
An object is an instance or representation of a class.
Objects are the building blocks of Object
Oriented Programming, objects give life to classes and structs. Classes are
mere definition of a model, the model
gets activated only when an object is created out of the class.
Here objCalculator is the
object which represents the class Calculator, the object objCalculator invokes
the Add() method of the class and passes values to the method. The object
performs the calculation which is defined in its base class definition and
returns the result of the calculation, which is captured and displayed in the textbox
txtResult in the client application.
We can create multiple instances of objects from a single class definition; each
object is unique and can hold its own set of values. The fields/properties of
an object are used to set/get values to the object while the methods of the object
are used to perform some operation on the object.
Let us consider the same Calculator class which we defined in the post What is a Class?, here we shall create an instance of the class, set values to the fields and invoke the methods of the object to see how it operates.
Calculator objCalculator = new Calculator();
Let us consider the same Calculator class which we defined in the post What is a Class?, here we shall create an instance of the class, set values to the fields and invoke the methods of the object to see how it operates.
Calculator objCalculator = new Calculator();
int nResult = objCalculator.Add(5, 6);
txtResult.Text = nResult.ToString();
What is a Class?
A Class is
the basic definition or template of an object, a class defined the properties,
methods, events etc of an object. A class is used to represent a real world
object/model. For example a calculator can be represented using a Calculator class,
the Calculator class defines the properties of a Calculator like input Numbers,
operations etc.
A Calculator class
just defines the properties and methods of an object, a class does not hold any
values of these properties, and to assign values to these we should create an
object which represents an instance of the class.
The following example shows a simple Calculator class
defined in C#
Subscribe to:
Posts (Atom)