Establishing Member Visibility:: Using Class
Establishing Member Visibility:: Using Class
using
System;
class
someclass
{
//Accessible anywhere.
public void
publicmethod()
{
Console.WriteLine("Visiblity of member is
public");
}
//ASsembly-protected access
protectedinternal void
protectedinternalmethod()
{
Console.WriteLine("Visiblity of member is
protectedinternal");
}
class helloclass
{
//c.protectedmethod(); Error
//c.privatemethod(); Error
//c.somemethod(); Error
Console.ReadLine();
return 0;
}
}
Output:
using System;
class program
{
public int myInt; // set to 0
Public string myString; //set to
null Public bool myBool; // set
to false
Public object myObj; //set to
null
class program
{
public int myInt=9;
Public string;
myString="hello";
Public bool
myBool=true;
Public object myObj=99.999;
using System;
class ConstData
{
public const string BestStudentTeam = "7 th ise";
public const double SimplePI = 3.14;
public const bool Truth =
true;
}
Referencing Constant Data:
class program
{
//Print local-level
const.
const int
LocalFixedValue=10;
Console.WriteLine("Local const: {0}",
LocalFixedValue);
Console.ReadLine();
}
}
Output:
con
st
A constant member is defined at compile time and cannot be
changed at runtime. Constants are declared as a field, using the const
keyword and must be initialized as they are declared. For example;
keyword.
To use a constant outside of the class that it is declared in, you
must fully qualify it using the class name.
readonly
A readonly member is like a constant in that it represents an
unchanging value. The
diference is that a readonly member can be initialized at runtime, in a
constructor as well being
able to be initialized as they are declared. For example:
public class MyClass
{
public readonly double PI = 3.14159;
}
or
public MyClass()
{
PI = 3.14159;
}
}
Note:
struct
MyRectangle
{
//The MyRectangle structure conatins a reference type
member
public MyRectangle(string
info)
{
rectInfo = new
ShapeInfo(info);
top = left =
10;
bottom = right =
100; }
}
class
program {
public static void
Main(string[] args) {
//create the first MyRectangle
Console.WriteLine("->Creating r1");
MyRectangle r1 =new MyRectangle("This is my first
rect");
//now assign a new MyRectangle to r1
Console.WriteLine("->Assigning r2 to
r1"); MyRectangle r2;
r2=r1;
//changing values of r2
Console.WriteLine("->Changing values of
r2"); r2.rectInfo.infoString="This is
new info"; r2.bottom=4000;
//print values
Console.ReadLine();
}
}
Output:
->Creating r1
->Assigning r2 to r1
->Changing values of 2
->Values after change
->r1.rectInfo.infoString
->r2.rectInfo.infoString
->r1.bottom:100
->r2.bottom :4000
using System;
class person {
public string fullName;
public int age;
public person() { }
class program
{
public static void SendAPersonByValue(person p)
{
//change the age of p
p.age = 99;//will the caller see this
reassignment
p = new person("nikki", 25);
}
public static void Main(string[ ]
args)
{
//passing reference types by value
Console.WriteLine("******passing person object by
value********
");
person smith = new person("smith", 10);
Console.WriteLine("Before by value call,
person is:"); smith.printInfo();
SendAPersonByValue(smith);
Console.WriteLine("After by value call, person
is:"); smith.printInfo();
Console.ReadLine();
}
}
Outpu
t:
using System;
class person
{
public string
fullName; public int
age;
public person() { }
SendAPersonByReference(ref smith);
Console.WriteLine("After by ref call, person
is:");
smith.printInfo();
Console.ReadLine();
}
}
Output:
int i=123;
object o = i; //boxing
class
program
{
Output:
The Object o1 = 10
The object o2 = 10
/*
static void UseBoxedMypoint(object
o)
{
Console.WriteLine(“{0},
{1}}”,o.x,o.y); }*/
static void UseBoxedMypoint(object o)
{
if (o is Mypoint)
{
Mypoint p = (Mypoint)o;
Console.WriteLine("{0},{1}", p.x,
p.y);
}
else
Console.WriteLine("You did not send a
Mypoint");
}
Output: 10,20
Example:
enum
EmpType {
Manager, // =0
Grunt, // =1
Contractor,// =2
VP // =3
}
Note: In C#, the numering scheme sets the first element to zero {0} by
default, followed by an n+1 progression.
// begin numbering at 102
enum
EmpType {
Manager = 102, // =
0 Grunt, // = 103
Contractor,// = 104
VP // = 105
}
Format() )
GetValues(
Returns
the string
associated
with the
enumerati
on.
Retrieves a
name (or
an array
containing
all names)
for the
constant in
the
specified
enumerati
on that has
the
specified
value.
Returns
the
members
of the
enumerati
on.
Descripti
on
Returns
whether a
given
string
name is a
member of
the current
enumerati
on.
using System;
using System;
enum EmpType
{
Manager, // = 0
Grunt, // = 1
Contractor, // = 2
VP // = 3
}
class program
{
public static void Main(string[] args)
{
//print information for the EmpType enumeration.
Array obj = Enum.GetValues(typeof(EmpType));
Console.WriteLine("This enum has {0} members:", obj.Length);
Output:
This enum has 4 members:
String name: Manager
int: (0)
hex: (00000000)
String name: Grunt
int: (1)
hex: (00000001)
String name: Contractor
int: (2)
hex: (00000002)
String name: VP
int: (3)
hex: (00000003)
using System;
enum EmpType
{
Manager, // = 0
Grunt, // = 1
Contractor, // = 2
VP // = 3
}
class program
{
public static void Main(string[] args)
{
//Does EmpType have a SalesPerson value?
if(Enum.IsDefined(typeof(EmpType), "SalesPerson"))
Console.WriteLine("Yes we have sales people");
else
Console.WriteLine("no sales people"); Console.ReadLine();
}
}
Output:
no sales people
In C#, an array index starts at zero. That means, first item of an array will be stored at
th
0 position. The position of the last item on an array will total number of items -
1.
In C#, arrays can be declared as fixed length or dynamic. Fixed length array can
stores a predefined number of items, while size of dynamic arrays increases as you add new
items to the array. You can declare an array of fixed length or dynamic.
The following code declares an array, which can store 5 items starting from index 0 to 4.
int [] intArray;
intArray = new int[5];
The following code declares an array that can store 100 items starting from index 0 to 99.
int [] intArray;
intArray = new int[100];
In C#, arrays are objects. That means declaring an array doesn't create an array. After
declaring an array, you need to instantiate an array by using the "new" operator.
The following code declares and initializes an array of three items of integer type.
int [] intArray;
intArray = new int[3] {0, 1, 2};
The following code declares and initializes an array of 5 string items.
string[] strArray = new string[5] {"Ronnie", "Jack", "Lori", "Max", "Tricky"};
You can even direct assign these values without using the new operator.
string[] strArray = {"Ronnie", "Jack", "Lori", "Max", "Tricky"};
Once you created an array, you are free to pass it as a parameter and receive it
as a member return vales.
Example:
using System;
class program
{
static void PrintArrays(int[ ] MyInt)
{
Console.WriteLine("The int array is");
for (int i = 0; i < MyInt.Length; i++)
Console.WriteLine(MyInt[i]);
}
Console.ReadLine();
}
}
Output:
10
20
30
40
Hello
What
That
stringarray
using System;
class program
{
Console.WriteLine();
}
Console.ReadLine();
}
0 0 0
0 1 2
0 2 4
The Array class, defined in the System namespace, is the base class for arrays in
C#.
Array class is an abstract base class but it provides CreateInstance method to construct an
array.
The Array class provides methods for creating, manipulating, searching, and sorting arrays.
Write C# program to demonstrate the methods of Array class i,e copy, sort, reverse, and clear
using System;
class program
{
System.Console.ReadLine();
}
}
Output:
Names are
amir
sharuk
salman
Hrithik
amir
sharuk
salman
Hrithik
sharuk
}
}
ob[0]=new employee("Ram","1AYIS01",300);
ob[1]=new employee("Raj","1AYIS02",400);
ob[2]=new employee("Rock","1AYIS03",500);
ob[3]=new employee("Rana","1AYIS04",600);
ob[4]=new employee("Raki","1AYIS05",700);
for(int i=0;i<5;i++)
{
ob[i]. display();
}
console.ReadLine();
}
}
A structure is a value type like a class, a structure can have its own fields, methods, and
constructors. Because structures are stored on the stack, as long as the structure is reasonably
small, the memory management overhead is often reduced.
Declaring a structure
To declare own structure type, use the struct keyword followed by the name of the type
and then enclose the body of the structure between opening and closing braces.
Syntactically, the process is similar to declaring a class. For example, here is a structure
named Time that contains three public int fields named hours, minutes, and seconds:
struct Time
{
public int hours, minutes, seconds;
}
As with classes, making the fields of a structure public is not advisable in most cases;
there is no way to control the values held in public fields. For example, anyone could set
the value of minutes or seconds to a value greater than 60.
A better idea is to make the fields private and provide your structure with constructors
and methods to initialize and manipulate these fields, as shown in this example:
struct Time
{
private int hours, minutes, seconds;
...
public Time(int hh, int mm, int ss)
{
this.hours = hh % 24;
this.minutes = mm % 60;
this.seconds = ss % 60;
}
public int Hours()
{
return this.hours;
}
}
When you copy a value type variable, you get two copies of the value. In contrast, when
you copy a reference type variable, you get two references to the same object.
for(int i=0;i<4;i++)
s[i].Hike_Marks();