c# -module3
c# -module3
Net
Module 3 :Structures- Defining a Structure, Assigning Values to Members ,
Structures with Methods, Nested Structures, Classes Vs Structures, Guidelines
to use Structures;
Enumerations- Enumerator Initialization, Enumerator Base Types, Enumerator,
Type Conversion.
Classes and Objects:
Classes, Constructors & Destructors, Nesting of Classes, Members, Properties.
C# - Structures
• In C#, a structure is a value type data type. It helps you to make a
single variable hold related data of various data types.
The struct keyword is used for creating a structure.
• Structures are used to represent a record. Suppose you want to keep
track of your books in a library. You might want to track the following
attributes about each book
• A structure in C# is simply a composite data type consisting of a
number elements of other types.
• A C# structure is a value type and the instances or objects of a
structure are created in stack.
• The structure in C# can contain fields, methods, constants,
constructors, properties, indexers, operators and even other structure
types.
Structure Declaration & Object Creation
Declare struct variable
• Before we use a struct, we first need to create a struct variable. We
use a struct name with a variable to declare a struct variable. For
example,
In the above example, we have created a struct named Employee. Here, we have
declared a variable emp of the struct Employee.
Access C# struct – Method1
struct MyStruct
{
int x = 20; // Error its not possible to initialize
int y = 20; // Error its not possible to initialize
}
• a struct can contain static fields, which can be initialized inside the
struct. The following example shows the use of static fields inside a
struct.
When we declare a
type member
as protected, it can
only be accessed from
the same class and its
derived classes.
Constructors in C#
• It is a special method present inside a class responsible for initializing
the variables of that class
• The name of the constructor method is exactly the same name as the
class in which it was present. You cannot change the name. If your
class name is Employee, then the name of the constructor method is
going to be Employee
• The constructor method does not return any value. That means it is a
non-value returning method
• Each and every class requires this constructor if we want to create the
instance of that class
It is the responsibility of a programmer to
define the constructor under his class name
and if he fails to do so , on behalf of the
programmer an implicit constructor gets
defined in that class by the compiler
after compilation, the compiler adds the public constructor to the class and
initializes the variable and this is the responsibility of a constructor i.e.
initializing the variables of that class.
• Every variable we declared inside a class and every field we declared inside a
class has a default value. All numeric types are initialized with 0, Boolean types
initialized with false, and string and object types initialized with null.
• There are five types of constructors available in C#, they are as follows
• Default or Parameter Less Constructor
• Parameterized Constructor
• Copy Constructor
C# Parameterized Constructor
• In C#, a constructor can
also accept parameters.
It is called a parameterized
constructor.,
Copy Constructor in C#:
• If we want to create multiple instances with the same values then we
need to use the copy constructor in C#, in a copy constructor the
constructor takes the same class as a parameter to it.
• By storing the value under a public variable, we can give direct access to the
value outside of the class.
• By storing that value in a private variable, we can also give access to that
value outside of the class by defining a property for that variable.
• Properties are the special type of class members that provides a
flexible mechanism to read, write, or compute the value of a private
field.
• It uses pre-defined methods which are “get” and “set” methods
which help to access and modify the properties.
Where,
• The syntax for Defining Properties: <access_modifier>
can be public,
private, protected
or internal.
<return_type> can
be any valid C#
type.
<property_name>
can be user-
defined.
Generally, Children have rights to their Parent’s Property. As a Child, tomorrow, I can take over my father’s
business. I can take over my Father’s Properties (Car, Buildings, Money, whatever it is). But I cannot take over my
father’s job. The reason is the may be based on his qualifications and his experiences Job whatever my father is
doing
the child class will consume all the members of the Parent class except the private members.
example Here, we have created class A with two public methods,
i.e., Method1 and Method2. Now, I want the same two
methods in another class, i.e., class B. One way to do this
is to copy the above two methods and paste them into
class B as follows.