How to access variables and functions of a specific class from another class in C#
Please give an example.
Answered by:- Sam
You can call a variable of first class from second class by creating Class constructor it is not a
static class for static class you can use
public class First
{
public string Name {get;set;}
}
//calling it from another class
class Program
{
First frst= new First();
static void Main(string[] args)
{
frst.Name = "Sam";
}
}
Check
MSDN
base class with property:
class Person
{
private string name; // the name field
public string Name // the Name property
{
get
{
return name;
}
set
{
name = value;
}
}
}
Check this url Auto-Implemented Properties (if advanced work on "name" isn't needed):
class Person
{
public string Name { get; set; } // the Name property with hidden backing field
}
Accessing class property from another class
Person person = new Person(); person.Name = "Joe"; // the set accessor is invoked here System.Console.Write(person.Name); // the get accessor is invoked here
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly