Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:04:05+00:00 2026-06-02T02:04:05+00:00

I have written a struct and functions where I try to pass the struct

  • 0

I have written a struct and functions where I try to pass the struct by reference (i.e. struct value can be modified inside functions).

enum RoomType { Economy, Buisness, Executive, Deluxe };

struct HotelRoom
{
    public int Number;
    public bool Taken;
    public RoomType Category;

    public void Print()
    {
        String status = Taken ? "Occupied" : "available";
        Console.WriteLine("Room {0} is of {1} class and is currently {2}", 
            Number, Category, status);
    }
}

Now to pass this struct by reference I’ve found two ways.

//Using Pointer
private unsafe static void Reserve(HotelRoom* room)
{
    if (room->Taken)
        Console.WriteLine("Cannot reserve room {0}", room->Number);
    else
        room->Taken = true;
}      

//Using ref keyword.
private static void Reserve(ref HotelRoom room)
{
    if (room.Taken)
         Console.WriteLine("Cannot reserve room {0}", room.Number);
    else
         room.Taken = true;
}    

Is there any difference? In general when should I use a pointer and when should I go for the ref keyword?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:04:06+00:00Added an answer on June 2, 2026 at 2:04 am

    Pointers are considered unsafe.

    If you code in an unsafe context, like passing a reference by pointer to the function, someone can change your pointer to pointer somewhere else, and you get garbage data.

    If you use the ref keyword, the context is “safe”, and you can’t change where room is pointing to, only it’s data like number, taken and category.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two window form applications written in C, one holds a struct consisting
I have written the following C99 code and was wondering about the struct declaration.
Summary: I have a struct that is read/written to file. This struct changes frequently,
I have a client written in Python for a server, which functions through LAN.
I have written a toolbar that runs on the taskbar. Unfortunately, after it is
I have written a class in python that implements __str__(self) but when I use
I have written a PHP application which requires storage of millions of integers between
I have written a program in VB6. When I compile it and send it
I have written a Python TCP/IP server for internal use, using win32serviceutil/py2exe to create
I have written a simple Java dispatcher with a daemon thread to handle the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.