Understanding C# structures practically step by step.
In this practical we will create a simple structure type using suitable name for a mailing list consisting of name, door number, street, city and pin-code. Develop a program that prompts the use to input these details and stores them in the structure, display details in appropriately.
Doing this practical is again a 7 step procedure, first create a simple c# console application, creating structure type with suitable name, creating properties in a structure, creating object for structure type, taking input values, display the output and finally to run an application.
Step 1:- Create simple C# application project
The first step is to create a simple c# console application project with a nice name as shown in the below figure. So start Microsoft visual studio, click on new project, select console application and press ok.
Once the project is created you will find some default libraries, namespace (Practical) a class with class name (Program) and an empty method with name "Main" as shown in the below code snippet.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Practical_6a
{
class Program
{
static void Main(string[] args)
{
……///code snippet
}
}
}
Step 2:- Creating Structure
To create a structure we need to use "struct" keyword and nice structure type name as show in below snippet of code.struct MailingList
{
…..…..
}
Step 3:- Creating Properties
First we need to create variables inside a structure for assigning to each property inside a structure type. Variables usually created using access modifier (Private, Public…) then followed by data type (string, int) then variable name as show in below snippet of code.struct MalingList
{
private string _name;
private int _doornumber;
private string _street;
private string _city;
private string _pincode;
}
Next step to create a property.To create a property is pretty simple. First choose access modifier (private, public…) the add data type (int, string, double…) finally property name.
The accessor of a property contains the executable statements getting (reading or computing) or setting (writing) the property. It can contain a get accessor, a set accessor, or both as shown in below snippet code.
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
Above snippet shows a property with public access modifier,string data type and property name (Name). Inside the curly braces of a property we use get and set accessors.In a get accessor return value for reading,in a set accessor we set the value.
Same way we need to create other properties in a structure for door number, street, city and pin-code.
Step 4:- Creating Structure Object
Now to create object for structure type .we need to take a structure name followed by object name then (=) sign followed by (new) keyword then structure name.static void Main(string[] args)
{
MailingList mlist = new MailingList();
}
We have created the (MailingList) object (mList) inside the Main method of a program.
Want a complete free solved hard copy call us @ 9967590707
Step 5:- Taking Input Values
Now taking an input values for name, door number, street, city and pin-code.
We need to write is to accept input number in the program and then assigning same numbers to different data type's variable. So to take the input number we need to use the "Console.ReadLine" function as shown in the below code snippet. We can use "Console.WriteLine" function to display "Please Enter Name For Mailing List".
When we take data from keyboard its always in string format, we need to convert that format to different data types as needed as shown in the below code snippet.static void Main(string[] args)
{
Console.WriteLine("Please Enter Name For Mailing List");
mlist.Name = Console.ReadLine();
Console.WriteLine("Please Enter Door Number For Mailing List");
mlist.DoorNumber = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Please Enter Street For Mailing List");
mlist.Street = Console.ReadLine();
Console.WriteLine("Please Enter City Name For Mailing List");
mlist.City = Console.ReadLine();
Console.WriteLine("Please Enter PinCode For Mailing List");
mlist.PinCode = Console.ReadLine();
}
Step 6:- Display the output
Once the taking input is completed we need to display the all variables. Using the "Consol.WriteLine" we can achieve the same easily as shown in the below code snippet.
// Display the output
Console.WriteLine("Display Details For Mailing List - {0} , {1}, {2} , {3}, {4} ", mlist.Name, mlist.DoorNumber, mlist.Street, mlist.City, mlist.PinCode);
Want a complete free solved hard copy call us @ 9967590707
Step 7:- Run the application
Finally run the application by hitting CNTRL + F5 and you should be able to run the application.