-3

i am writing a simple code by using ref keyword. As i understand struct can be really slow on copying things. In order to make it faster you should use ref. So i wrote this simple code below.

using System;

namespace ConsoleApplication4
{
    class Program
    {
        public static void returns(ref s s1)
        {
            for (int i = 0; i < 100;i++)
            {
                s1.z += i;
            }
        }
        static void Main(string[] args)
        {
            s s1 = new s();
            returns(ref s1);
        }
    }
}

it gave me the error "Error 1 Inconsistent accessibility: parameter type 'ref is less accessible than method". I checked one of stackoverflow question. Using ref seemed like that. What is my mistake here. Can you give me any advice.

Thanks in advance.

AlexanderBrevig
  • 1,967
  • 12
  • 17
  • 8
    This means that the type `s` is *not* public. It has nothing to do specifically with passing `struct` by reference. – Sergey Kalinichenko Sep 18 '14 at 13:18
  • "whenever you pass a struct as a parameter or assign a struct to another struct (as in A=B, where A and B are structs), the full contents of the struct are copied." The tuto i read meantioned about this. – Berat Emre Nebioglu Sep 18 '14 at 13:24
  • 1
    @BeratEmreNebioglu That is correct. If that is a concern for your application .. then you should be using a `class`. – Simon Whitehead Sep 18 '14 at 13:25
  • -1 for not including `struct s` in the question – H H Sep 18 '14 at 13:37
  • i just learn c# i did java but dont remember i used "ref". @SimonWhitehead are you suggesting using class instead of using struct with ref. Also i heard for a group of variable that doesnt exceed 16bit as sum like integer is 4 bytes. Four integer is 16 bytes. You should use struct. Do you agree with this? – Berat Emre Nebioglu Sep 18 '14 at 13:42
  • By default, you should use `class` in c#. `struct`, while useful, has a number of gotchas and pitfalls that are not for beginners. – crashmstr Sep 18 '14 at 14:08
  • @crashmstr is there any source or link meantioned above "gotchas and pitfalls". I want to learn this language in real deep. – Berat Emre Nebioglu Sep 18 '14 at 14:24
  • @BeratEmreNebioglu [Why are mutable structs evil?](http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil/441323#441323), [When to use struct in C#?](http://stackoverflow.com/questions/521298/when-to-use-struct-in-c) – crashmstr Sep 18 '14 at 14:26
  • @crashmstr thank you very much i appreciated. So may be another subject but i need to ask String and String.textBilder String is immutable and textBuilder is mutable so we should choose string then. Because it is immutable? – Berat Emre Nebioglu Sep 18 '14 at 14:56

2 Answers2

2

Likely the type s is not a public type.

plinth
  • 48,267
  • 11
  • 78
  • 120
0

oh thanks i just forgot to type these lines to here.

public struct s
    {
        public int z;
    }

i did this private not i changed to a public. One of my friend suggest me to do private struct blah and do property of struct public. Is it false logic.

  • Your friend should have suggested to avoid `struct`s. And certainly mutable structs. Find a tutorial that starts with classes. – H H Sep 18 '14 at 13:37
  • okey i am reading a good book i guess it is covering both subjects. Thanks for suggestions @HenkHolterman. – Berat Emre Nebioglu Sep 18 '14 at 13:45