473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Learners question: Static methods

Hi all,

I am new to c#. I was trying out the following:

class A
{
private static void doSomething()
{
System.Console.WriteLine("Class A is doing something");
}
}

class B
{
private static void Main()
{

A.doSomething();
}
}
If I compile the above code, I get the following error:

error CS0122: 'A.doSomething()' is inaccessible due to its
protection level

If I remove the private keyword from the method doSomething() in class A,
the file gets compiled without errors.
The error is clear to me. But what is not clear is why the compiler does not
give error for the method Main(). It is also a static method which I have
decleared private which should mean its not accessible to anybody.

Thanks in advance,
Madhu.

Nov 17 '05 #1
4 1114
Sorry, I forgot to mention in my mail that the code gets compiled only if I
specify public keyword.

"Madhu" <ma***@dreamsland.ch> wrote in message
news:OI**************@tk2msftngp13.phx.gbl...
Hi all,

I am new to c#. I was trying out the following:

class A
{
private static void doSomething()
{
System.Console.WriteLine("Class A is doing something");
}
}

class B
{
private static void Main()
{

A.doSomething();
}
}
If I compile the above code, I get the following error:

error CS0122: 'A.doSomething()' is inaccessible due to its
protection level

If I remove the private keyword from the method doSomething() in class A,
the file gets compiled without errors.
The error is clear to me. But what is not clear is why the compiler does
not give error for the method Main(). It is also a static method which I
have decleared private which should mean its not accessible to anybody.

Thanks in advance,
Madhu.

Nov 17 '05 #2
rtfm :)

Nov 17 '05 #3

"Madhu" <ma***@dreamsland.ch> wrote in message
news:e%***************@TK2MSFTNGP09.phx.gbl...
Sorry, I forgot to mention in my mail that the code gets compiled only if
I
specify public keyword.

"Madhu" <ma***@dreamsland.ch> wrote in message
news:OI**************@tk2msftngp13.phx.gbl...


I found such explanation of fact that
private static int Main() works.

"Author:Roy Fine (rl****@twt.obfuscate.net)
Topic:Re: private static void Main()
News grup: microsoft.public.dotnet.languages.csharp
Date:2004-03-07 10:38:33 PST
Sujala,

public, private, protected and others are access attributes that are
enforced primarily by the compiler (at runtime only when dynamic invocation
is involved). Beyond that, the runtime is free to accept or ignore the
access specifiers - and in the case of Main, it kindly ignores them.

Matters such as these (and this is but one example of many others) are
implementation specific details for the runtime, not for a particular
language.

regards
roy fine
"

--
Artur Borecki, MCP
Nov 17 '05 #4
"Madhu" <ma***@dreamsland.ch> wrote in message
news:e%***************@TK2MSFTNGP09.phx.gbl...
Sorry, I forgot to mention in my mail that the code gets compiled only if
I
specify public keyword.

"Madhu" <ma***@dreamsland.ch> wrote in message
news:OI**************@tk2msftngp13.phx.gbl...
Hi all,

I am new to c#. I was trying out the following:

class A
{
private static void doSomething()
{
System.Console.WriteLine("Class A is doing something");
}
}

class B
{
private static void Main()
{

A.doSomething();
}
}
If I compile the above code, I get the following error:

error CS0122: 'A.doSomething()' is inaccessible due to its
protection level

If I remove the private keyword from the method doSomething() in class A,
the file gets compiled without errors.
The error is clear to me. But what is not clear is why the compiler does
not give error for the method Main(). It is also a static method which I
have decleared private which should mean its not accessible to anybody.


I'm still learning all this stuff, but I'll give it a shot...

Declaring the Main method as private static should indicate that is
available to other members of the type containing Main. However, declaring
your type as private static would make the type and all of its members
completely inaccessible.

carl
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Tom Dacon | last post by:
I'm curious to see if anyone has an opinion on this little design question - I'm doing a computational astronomy library in C#, purely for my own use, and one of the things that happens regularly...
5
by: allison | last post by:
Can someone point me to a guide on when to use static methods versus instance methods in a class? For instance, is it bad design to have a static method that creates an instance of another class? ...
3
by: Amadelle | last post by:
Hi All and thanks in advance, I wanted to know when is a good idea to use a static class (with static constructor) and when to use instance classes? I have read couple of articles on line and...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when...
6
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
15
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data...
18
by: Wiktor Zychla [C# MVP] | last post by:
Following code was supposed to sort an ArrayList of strings while keeping one "fixed" element at the last position. However, the code hangs in .NET 1.1. I think that answering these questions...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.