You are on page 1of 3

3/3/23, 7:59 PM Worksheet 1.

1: Attempt review

My courses

Started on Friday, 3 March 2023, 7:53 PM


State Finished
Completed on Friday, 3 March 2023, 7:59 PM
Time taken 5 mins 55 secs

https://lms.cuchd.in/mod/quiz/review.php?attempt=1111516&cmid=1039146 1
3/3/23, 7:59 PM Worksheet 1.1: Attempt review

Question 1
Complete

Marked out of

Do any one

1. Suppose we have class program and it contains the fields like name and roll no and these fields are
private fields and we want to access that outside the class. Write a program to access these fields.

2. Suppose we have class program and it contains the fields like name and roll no and these fields are
private fields and we want to assign values to that outside the class. Write a program to assign values to
these fields

using System;

class Program

{
private string name;

private int rollNo;

public string Name

{
get { return name; }

set { name = value; }

public int RollNo

{
get { return rollNo; }

set { rollNo = value; }

static void Main(string[] args)


{

Program obj = new Program();

obj.Name = "vishvadeep singh chauhan";

obj.RollNo = 20bca1146;

Console.WriteLine("Name: " + obj.Name);


Console.WriteLine("Roll No: " + obj.RollNo);

}
}

In this program, we have a class called "Program" with private fields for name and roll number. We also have
properties for each field, which allow us to get and set the values of the fields outside of the class.

In the Main method, we create an object of the Program class and use the properties to set the name and roll
number. We then use the properties again to retrieve and print the values.

output:-

Name: vishvadeep singh chauhan


Roll No: 20bca1146

https://lms.cuchd.in/mod/quiz/review.php?attempt=1111516&cmid=1039146 2
3/3/23, 7:59 PM Worksheet 1.1: Attempt review

This is because the program creates an instance of the "Program" class and sets the "Name" and "RollNo" properties of that instance to "

Previous activity

Jump to...

Next activity

https://lms.cuchd.in/mod/quiz/review.php?attempt=1111516&cmid=1039146 3

You might also like