This maybe a very simple question! But I have been scratching my head for an hour now! I have two files as below:
Assembly1.cs
Program.cs
I thought when I use internal keyword before a class name I won't be able to instantiate it in other classes, right?
https://msdn.microsoft.com/en-us/library/7c5ka91b.aspx
But why am I not getting an error message for this here? I maybe missing something very obvious here.
// Assembly1.cs
namespace InternalTest
{
internal sealed class BaseClass
{
public static int intM = 0;
}
}
// Program.cs
using System;
namespace InternalTest
{
class Program
{
static void Main(string[] args)
{
var myBase = new BaseClass();
Console.ReadKey();
}
}
}
Internal means class is accessible within the assembly.Above class is in same assembly hence no error.If you want to see the error then follow below step
1) Create Library project
2) Create Base Class in that project
3) Create Console project
4) Add reference of first project dll
5) Now try to create instance, you will see the error
Additional Info
If you want to access internal members in your console project then add below attribute in AssemblyInfo.cs of Library project
[assembly:InternalsVisibleTo("[AssemblyNameOfConsoleProject]")]
Because both classes are on the same namespace.
//InternalTest
namespace InternalTest
{
internal sealed class BaseClass
{
public static int intM = 0;
}
}
//the same here InternalTest
namespace InternalTest
{
class Program
{
static void Main(string[] args)
{
}
}
}
Related
Hi trying to make a class inside a static class to use in JINT but when it's referenced I get an error
C# code
namespace Hi {
public static class Ok {
public class Wowa {
public Wowa(){}
}
}
}
But when I try to make a new one in JavaScript I get an error "the object cannot be used as a constructor" from JINT
var k = new Hi.Ok.Wowa()
Am I doing this right? How can I set up the C# to be able to use the above code in JavaScript from JINT?
BTW IF instead of "Ok" being a static class, rather a namespace, it works, but I want it as a class because I want to have static methods in it also
you cant use none-static class in a static class (ReadThis) but if you remove (static) in your frist class
namespace Hi {
public class Ok {
public class Wowa {
public Wowa(){}
}
}
}
and it can be said that it does not make much difference because (Static) only makes subcategories of your class have to use (Static).
But if you want your class to be impossible to build on variables, you can use abstract(ReadThis)
namespace Hi {
public abstract class Ok {
public class Wowa {
public Wowa(){}
}
}
}
and
Main()
{
Ok k = new Ok();//Error
}
Imagine you have this:
namespace Hi
{
public static class Ok
{
public class Wowa
{
public Wowa() { }
public static string MyStaticMethod() => "Hello from 'Static Method'";
public string MyNormalMethod() => "Hello from 'Normal Method'";
}
}
}
It's possible to use non-static class Wowa by making an instance of it , and then you can call MyNormalMethod of that instance (you can only call not-static method within instance of that class).
Hi.Ok.Wowa wowa = new Hi.Ok.Wowa();
wowa.MyNormalMethod();
And without making any instance of Wowa you can call static method within it, like this:
Hi.Ok.Wowa.MyStaticMethod();
Finally you can see working code here.
I was reading about virtual method tables on wikipedia and I stumbled upon
Whenever a class defines a virtual function (or method), most compilers add a hidden member variable to the class which points to an array of pointers to (virtual) functions called the virtual method table (VMT or Vtable).
However, when I decompile my test code I don't find any member variable in the IL.
I am using latest version of Roslyn to compile (2.6.0).
Here is my test code:
namespace ConsoleApp1
{
using System;
internal class Program
{
private static void Main(string[] args)
{
var a = new Base();
var b = new Derived();
a.Say();
b.Say();
}
}
public class Base
{
public virtual void Say()
{
Console.WriteLine($"{this.GetType()}");
}
}
public class Derived : Base
{
public override void Say()
{
Console.WriteLine($"{this.GetType()}");
}
}
}
I suppose I am misunderstanding something here, could you help me what it is?
I have the follwoing code which I don't understand where is my mistake?
Here's the code:
using System;
using System.Collections;
namespace ConsoleApplication5
{
static void Main(string[] args)
{
class Program
{
public class Animal
{
public virtual void Greet()
{
Console.WriteLine("Hello, I'm some sort of animal!");
}
}
public class Dog : Animal
{
public override void Greet()
{
Console.WriteLine("Hello, I'm a dog!");
}
}
}
It doesn't get compiled, I get the following errors:
On Line 24 Type or namespace definition, or end-of-file expected
and on Line 6 } is expected
I don't understand what did I do wrong, can you help me?
Thanks.
I compiled this in Visual Studio 2015, if it matters.
BTW, I got part of this code from this tutorial:
http://csharp.net-tutorials.com/classes/inheritance/
I believe you have a confusion about scopes. You defined a class in a method which is not applicable, you should define classes under namespace scope.
It should be this way (This is a pseudo code)
namespace YourNamespace
...class Program
.... public class animal
.... public class dog : animal
.... static void main
You cannot define a class within a method body, which is what happens here:
static void Main(string[] args)
{
class Program
{
Instead you need to place the code for your Main() method within the Program class:
class Program
{ // This brace marks the beginning of the Program class
static void Main(string[] args) // This is a method defined WITHIN Program class
{ // The brace marks the beginning of the Main method
....
} // This brace marks the end of the Main method
} // This brace closes the Program class
In VS, I have referenced a DLL of a library.
I would like to use my own definition of a class that the library/api uses instead of the library's. I want to do this to add functionality to existing classes.
Class extensions do not suffice because I want to add new static methods to classes.
I have the source of the classes I want to reimplement.
Is there a way to redefine the class in my project and use it instead of the dll's class without importing the entire source of the library/dll. I'd rather not import the entire source, just the classes I want to modify.
In fact, the classes I want to modify are not classes but structs, and do not have the partial modifier.
Try extension methods:
namespace DLLNamespace
{
public struct TestStruct
{
public string Name { get; set; }
public void SetName(string name) { this.Name = name; }
}
}
namespace ProgramNamespace
{
public static class ExtensionMethods
{
public static void ReverseName(this DLLNamespace.TestStruct target)
{
target.Name = new string(target.Name.ToArray().Reverse().ToArray());
}
}
public class Program
{
static void Main(string[] args)
{
DLLNamespace.TestStruct ts;
ts.SetName("John");
ts.ReverseName();
Console.WriteLine(ts.Name);
}
}
}
There must be something simple that I'm missing. I have two partial classes, in two different assemblies, and I'm not able to reference a private static method in one partial class from the other partial class.
For example, in the FirstHalf assembly, I have the following class:
namespace FirstHalf
{
public partial class TestPartialClass
{
private static void PrintFoo()
{
Console.WriteLine("Foo");
}
}
}
Then, in the SecondHalf assembly, I have the following class:
namespace FirstHalf
{
public partial class TestPartialClass
{
public static void Main(string[] args)
{
Console.WriteLine(PrintFoo());
}
}
}
However, when I try to invoke PrintFoo() from the SecondHalf assembly, I get the following error:
CS0103: The name 'PrintFoo' does not exist in the current context.
What's going on, here? I have a reference from SecondHalf to FirstHalf, so Visual Studio does know that there is a dependency between the two.
You can't split a partial class between two assemblies; they are actually being compiled as two different classes.
If you really want to accomplish something like this across assemblies, with sharing of 'private' members, you can get something similar by creating a base class and inheriting it:
namespace FirstHalf
{
public class Base
{
protected static void PrintFoo()
{
Console.WriteLine("Foo");
}
}
}
namespace SecondHalf
{
public class Derived : FirstHalf.Base
{
public static void Main(string[] args)
{
PrintFoo();
}
}
}
That said, there is probably a cleaner way to accomplish what you're trying to do using some form of composition; the details depend on your particular application.