0% found this document useful (0 votes)
7 views8 pages

Module 2

The document provides an overview of the C# object model, explaining the concepts of objects and classes as fundamental components of the language. It covers the creation of objects, the structure of classes, the use of the 'this' keyword, nested classes, partial classes and methods, access modifiers, value types, enumerations, structures, and arrays. Additionally, it highlights the advantages and disadvantages of arrays in C# programming.

Uploaded by

batch0406sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Module 2

The document provides an overview of the C# object model, explaining the concepts of objects and classes as fundamental components of the language. It covers the creation of objects, the structure of classes, the use of the 'this' keyword, nested classes, partial classes and methods, access modifiers, value types, enumerations, structures, and arrays. Additionally, it highlights the advantages and disadvantages of arrays in C# programming.

Uploaded by

batch0406sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Understanding the C# object model

C# Object and Class

Since C# is an object-oriented language, program is designed using objects and classes in C#.
C# Object
In C#, Object is a real-world entity, for example, chair, car, pen, mobile, laptop etc. In other words,
object is an entity that has state and behaviour. Here, state means data and behaviour means
functionality. Object is a runtime entity; it is created at runtime.
Object is an instance of a class. All the members of the class can be accessed through object.

Let's see an example to create object using new keyword.


Student s1 = new Student ();//creating an object of Student
In this example, Student is the type and s1 is the reference variable that refers to the instance of
Student class. The new keyword allocates memory at runtime.

C# Class
In C#, class is a group of similar objects. It is a template from which objects are created. It can have
fields, methods, constructors etc.

Let's see an example of C# class that has two fields only.


class <Class_name> {
<access_modifier> [static] variable_type fields, constants
<access_modifier> [static] return_type methodName(args..){ - - - - }
... constructors, destructors ...
... properties ...// for component-based programming
... events ...
... indexers ... // for convenience
... overloaded operators ...
... nested types (classes, interfaces, structs, enums, delegates)
}

C# Object and Class Example


Let's see an example of class that has two fields: id and name. It creates instance of the class,
initializes the object and prints the object value.

using System;
public class Student
{
int id;//data member (also instance variable)
String name;//data member(also instance variable)
public static void Main(string[] args)
{
Student s1 = new Student();//creating an object of
Student s1.id = 101;
s1.name = "ABC";
Console.WriteLine(s1.id);
Console.WriteLine(s1.name);
}
}

Output:
101 ABC

Using this keyword:


The “this” keyword refers to the current instance of a class. With the “this” keyword, you can access an
instance of a class and use it with instance methods, instance constructors, and instance properties
using System;
public class Employee
{
public int id;
public String name;
public float salary;
public Employee(int id, String name,float salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
public void display()
{
Console.WriteLine(id + " " + name+" "+salary);
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee(101, "Sanjay", 890000f);
Employee e2 = new Employee(102, "Mahesh", 490000f);
e1.display();
e2.display();

}
}

Using the Nested Classes:


A Nested class is a class that is defined inside another class. This class acts as the member of the
parent class in which it is defined.
Advantage: Accessing all the members of its outer class.

C# Nested Class
In C#, we can define a class within another class. It is known as a nested class. For example,

class OuterClass {
...
class InnerClass {
...
}
}

C# - Partial Classes and Methods


C# contains a special method is known as a partial method, which contains declaration part in one
partial class and definition part in another partial class or may contain both declaration and
definition in the same partial class.
Basically, partial methods exist in the partial class, or in the struct.

Syntax:
partial void method_name
{
// Code
}

circle1.cs
public partial class Circle {

// This file only contains


// declaration of partial method
partial void area(int p);

public void Display()


{
Console.WriteLine("Example of partial method");
}
}

circle2.cs
public partial class Circle {

public void newarea(int a)


{
area(int a);
}

// This is the definition of


// partial method
partial void area(int r)
{
int A = 3.14 * r * r;
Console.WriteLine("Area is : {0}", A);
}
}

When we execute the above code, then compiler combines circle1.cs and circle2.cs into a single file,
i.e. circle as shown below.
circle
Public class Circle {
public void Display()
{
Console.WriteLine("Example of partial method");
}
public void newarea(int a)
{
area(int a);
}
private void area(int r)
{
int A = 3.14 * r * r;
Console.WriteLine("Area is : {0}", A);
}
}

Returning a Value from a Method:


A method can return a value to the caller of the method.
If the return type is void, then the method will not return any value.
If the return type is other than void, such as int, double, string or class_type then the method can
return any type of value using return keyword.

using System;
namespace Class_Demos{
class MyArea {
double l, b, area;
public double RectArea() {
Console.WriteLine("Enter the Length");
l = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the Breadth");
b = double.Parse(Console.ReadLine());
area = l * b;
return (area);
}
}
class ReturnValue{ static void Main(){
MyArea obj = new MyArea();
double area1= obj.RectArea();
Console.WriteLine("Area of the Rectangle:"+area1);
Console.ReadLine();
}}}

Describing Access Modifiers:


Access modifiers help to avoid jumbling of data and methods with the existing code as well as
protect an object of a class from outside interference. These modifiers do this by defining a certain
scope to access data and methods in a restricted manner.
Table 1.1: List of the access modifiers

Access Speicifiers:

1. public: The type or member can be accessed by any other code in the same assembly or
another assembly that references it. The accessibility level of public members of a type is
controlled by the accessibility level of the type itself.
2. private: The type or member can be accessed only by code in the same class or struct.
3. protected: The type or member can be accessed only by code in the same class, or in a class
that is derived from that class.
4. internal: The type or member can be accessed by any code in the same assembly, but not
from another assembly. In other words, internal types or members can be accessed from
code that is part of the same compilation.
5. protected internal: The type or member can be accessed by any code in the assembly in
which it's declared, or from within a derived class in another assembly.
6. private protected: The type or member can be accessed by types derived from the class that
are declared within its containing assembly.
Creating Value Type using enum & structures:
A data type is a value type if it holds a data value within its own memory space. It means the
variables of these data types directly contain values.
For example, consider integer variable int i = 100;

The following data types are all of value type:


Bool, byte, char, decimal, double, enum, float, int, long, sbyte, short, struct, uint, ulong & ushort.

Enumerations and structures in c#:

An enum is a special "class" that represents a group of constants (unchangeable/read-only variables).


An enumeration type (or enum type) is a value type defined by a set of named constants of the
underlying integral numeric type. To define an enumeration type, use the enum keyword and specify
the names of enum members:
enum Season
{
Spring,
Summer,
Autumn,
Winter
}

Structures (struct):
A structure is a value type that allows you to group related data together.

Syntax:
Access_Modifier struct structure_name {
// Fields
// Parameterized constructor
// Constants
// Properties
// Indexers
// Events
// Methods etc.
}
The following table shows the primitive types in C# and their equivalent types in the Microsoft .NET
Framework. Notice that the string and object types are classes (reference types) rather than
structures.

C# Arrays:
Like other programming languages, array in C# is a group of similar types of elements that have
contiguous memory location. In C#, array is an object of base type System.Array. In C#, array index
starts from 0. We can store only fixed set of elements in C# array.
Advantages of C# Array:
Code Optimization (less code)
Random Access
Easy to traverse data
Easy to manipulate data
Easy to sort data etc.

Disadvantages of C# Array
Fixed size

C# Array Types
There are 3 types of arrays in C# programming:
Single Dimensional Array
Multidimensional Array
Jagged Array

You might also like