2 Assignment
2 Assignment
1) What is object? How to initialize object and how to call object from Main
method?
In C#, an object is an instance of a class. A class serves as a blueprint for creating objects,
defining their structure and behavior.
Initialize an object:
You initialize an object using the new keyword followed by the class name, optionally
passing any required parameters to the constructor.
MyClass myObject = new MyClass(42);
Call object methods:
You call object methods using the dot (.) operator after the object name.
myObject.MyMethod();
Example:
using System;
// Define a simple class
class Person
{
// Properties
public string Name { get; set; }
public int Age { get; set; }
// Method to introduce the person
public void Introduce()
{
Console.WriteLine($"Hi, my name is {Name} and I am {Age} years old.");
}
}
class Program
{
static void Main(string[] args)
{
// Create an object of the Person class
Person person1 = new Person();
// Initialize the object's properties
person1.Name = "John";
person1.Age = 30;
// Call a method on the object
person1.Introduce();
}
}
Output:
Hi, my name is John and I am 30 years old.
2) What is class? How to define class in c# with example?
In C#, a class is a blueprint for creating objects. It defines the properties, methods, events,
and fields that a particular type of object can have.
Syntax:
class ClassName
{
// Fields (optional)// Properties// Constructors // Methods
}
Output:
Hi, my name is John and I am 30 years old.
Hi, my name is Alice and I am 25 years old.
3) Difference between class and object?
class object
Class is used template for an object. An object is an instance of class.
A class is a logical entity. Object is a physical entity.
It is declared with the class keyword. It is created with a class name with the
new keyword.
When class is created no memory is Objects are created memory space,
allocated. whenever they are created.
Class can not be manipulated. Objects can be manipulated.
Example:
using System;
class Program
{
static void ModifyValue(int x)
{
x = x * 2;
Console.WriteLine("Inside ModifyValue method: " + x);
}
static void Main(string[] args)
{
int num = 10;
Console.WriteLine("Before calling ModifyValue method: " + num);
ModifyValue(num);
Console.WriteLine("After calling ModifyValue method: " + num);
}
}
Output:
Before calling ModifyValue method: 10
Inside ModifyValue method: 20
After calling ModifyValue method: 10
Output:
Before calling ModifyReference method: John
Inside ModifyReference method: Alice
After calling ModifyReference method: Alice
5) What is enumeration? Write it’s uses with syntax.
An enumeration, or enum, in C#, is a value type data type that consists of a set of named
constants called enumerators. These enumerators are assigned integral constant values,
usually starting from 0, but you can specify custom values if needed. Enums provide a way
to define a set of related named constants, making the code more readable and maintainable.
Syntax:
enum EnumName
{
Enumerator1,
Enumerator2,
// More enumerators
}
Uses of Enumeration:
Improving Code Readability:
Enumerations provide descriptive names to represent integral values, making the code easier
to understand.
Type Safety:
Enumerations offer type safety because the compiler checks that values assigned to variables
of the enumeration type are within the defined set of constants.
Avoiding Magic Numbers:
Enumerations help avoid using magic numbers in code by giving meaningful names to
integral constants.
Switch Statements:
Enumerations are often used in switch statements for better readability and maintainability.
Configuration and Settings:
Enumerations are commonly used to represent configuration settings or options with
predefined values.
Output:
Today is: Wednesday
Happy Hump Day!
6) Write a program value type with enumeration?
using System;
enum Fruit // Enumeration representing different types of fruits
{
Apple,
Banana,
Orange
}
class Program
{
static void DisplayFruitInfo(Fruit fruit) // Method to display information about a fruit
{
switch (fruit)
{
case Fruit.Apple:
Console.WriteLine("Name: Apple");
Console.WriteLine("Color: Red or Green");
Console.WriteLine("Taste: Sweet or Tart");
break;
case Fruit.Banana:
Console.WriteLine("Name: Banana");
Console.WriteLine("Color: Yellow");
Console.WriteLine("Taste: Sweet");
break;
case Fruit.Orange:
Console.WriteLine("Name: Orange");
Console.WriteLine("Color: Orange");
Console.WriteLine("Taste: Sweet and Tangy");
break;
default:
Console.WriteLine("Unknown fruit");
break;
}
}
static void Main(string[] args)
{
Fruit myFruit = Fruit.Banana; // Declare a variable of type Fruit
Console.WriteLine("Information about my fruit:");
DisplayFruitInfo(myFruit); // Display information about the fruit
}
}
Output:
Information about my fruit:
Name: Banana
Color: Yellow
Taste: Sweet
7) What is structure? Use of structure and it’s syntax?
A structure in C# is a value type that can contain data members of different data types.
Syntax:
struct StructName
{
// Data members (fields)
// Methods (optional)
}
Use of Structure:
Pointers: Representing geometric points in two or three-dimensional space.
Complex Numbers: Storing complex numbers with real and imaginary parts.
Date and Time: Representing dates and times using separate fields for year, month, day,
hour, minute, and second.
Coordinates: Storing geographic coordinates such as latitude and longitude.
Color: Storing RGB values for representing colors.
Example:
using System;
// Define a structure to represent a point in 2D space
struct Point
{
public int X; // Data members
public int Y;
public Point(int x, int y) // Constructor
{
X = x;
Y = y;
}
// Method to display the coordinates of the point
public void DisplayCoordinates()
{
Console.WriteLine($"X: {X}, Y: {Y}");
}
}
class Program
{
static void Main(string[] args)
{
// Create an instance of the Point structure
Point point1 = new Point(10, 20);
// Display the coordinates of the point
Console.WriteLine("Coordinates of point1:");
point1.DisplayCoordinates();
}
}
Output:
Coordinates of point1:
X: 10, Y: 20
8) Write a program structure using value type?
using System;
// Define a structure to represent a person
struct Person
{
// Data members
public string Name;
public int Age;
// Constructor
public Person(string name, int age)
{
Name = name;
Age = age;
}
class Program
{
static void Main(string[] args)
{
// Create instances of the Person structure
Person person1 = new Person("John", 30);
Person person2 = new Person("Alice", 25);
Output:
Information about person1:
Name: John, Age: 30
Information about person2:
Name: Alice, Age: 25
9) What is array? Uses of array?
An array in C# is a collection of elements of the same type that are stored in contiguous
memory locations.
Uses of Arrays:
Grouping Data:
Arrays are used to group together elements of the same type under a single name.
Sequential Access:
Elements in an array are accessed using an index, allowing for easy traversal and
manipulation of data.
Efficient Storage:
Arrays provide efficient memory allocation and access, making them suitable for storing
large amounts of data.
Iterating and Manipulating Data:
Arrays allow for easy iteration over elements, making tasks like sorting, searching, and
filtering data straightforward.
Passing and Returning Multiple Values:
Arrays can be passed as arguments to methods or returned as function results to encapsulate
and transfer multiple values.
Example:
using System;
class Program
{
static void Main(string[] args)
{
// Define an array of integers
int[] numbers = { 10, 20, 30, 40, 50 };
Output:
Elements of the array:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50
10) Write a program for single dimensional and multidimensional array?
using System;
class Program
{
static void Main(string[] args)
{
// Single-dimensional array
int[] singleArray = { 1, 2, 3, 4, 5 };
Console.WriteLine("Single-dimensional array:");
foreach (int num in singleArray)
{
Console.Write(num + " ");
}
Console.WriteLine();
Output:
Single-dimensional array:
12345
Enumeration array
Enumeration is a data type that consists An array is a data structure that stores a
of a set of named constants. collection of elements of the same type in
contiguous memory locations.
It represents a list of predefined values It represents a sequence of values
(constants) that can be assigned to indexed by integers, allowing random
variables. access to elements based on their index.
Enumerations are useful for defining a Arrays are useful for storing and
collection of related named constants, manipulating a fixed-size sequential
making the code more readable and collection of elements.
maintainable.
Example: Days of the week (Sunday, Example: Collection of integers [1, 2, 3,
Monday, Tuesday, etc.) 4, 5]