Module 2
Module 2
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.
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.
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
}
}
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 {
...
}
}
Syntax:
partial void method_name
{
// Code
}
circle1.cs
public partial class Circle {
circle2.cs
public partial class Circle {
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);
}
}
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();
}}}
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;
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