Sadi02’s Weblog

June 1, 2008

Class Modifiers and their uses in C# .Net

Filed under: Computer Science — Tags: , , — Md. Shaik Sadi @ 6:56 am

Class/Interface (Base class)

Inheritance (Inherited class)

Implementation (Interface)

(Child)

(Instance)

Is initiated??

General

Abstract

Sealed

Static*

General

Yes

Yes

Yes

No

No

Yes

Abstract

Yes

Yes

Yes

No

No

No

Sealed

No

No

No

No

No

Yes

Static

No

No

No

No

No

No

Interface

Yes

Yes

Yes

No

Yes

No

 

 

 


 

 

 

 

 

This table is for showing relationship between a base class and child class

In this table I want to show the accessibility among the various types of classes and interface. From this table you also can know what type of class can be initiated. This is one example that helps you to understand the table.

If you use Abstract class as base class and take a general class as child and want to inherit, this will possible …here you can see..

Base: Abstract (ASadi)

Child: Interface (Sadi)

Accessibility: Yes

abstract class Sadi : ASadi

{

#region ASadi Members

public int getint() {

throw new Exception(“The method or operation is not implemented.”); }

#endregion

}

If you execute this code, it executes properly

From the first column of the table we can say that general class inherites both general and abstract class and also implements an interface. These classes cannot inherites sealed as well as static class. Only General and Sealed classes can be initiated.

From the table and above discussion we can conclude that,

Abstract Class

A class defined as abstract is used as a base class. Such a class is used for the purpose of inheritance only i.e. other classes are derived from this class. We cannot create an object of an abstract class. An abstract class may contain methods and properties. The classes derived from the abstract class inherit these methods and properties. The abstract class may also contain abstract methods and properties. Abstract method and properties do not have any functionality. The derived class defines their full functionality.

Here is an example of an abstract class:

abstract class MyAbstract { public abstract void AbMethod(); }

Sealed Class

Classes can be declared as sealed. This is accomplished by putting the sealed keyword before the keyword class in the class definition. For example:

public sealed class classSealed { // Class members here. public string ID; public double Price; }

A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster. Sealing a class means one cannot derive from it. Sealing a method means one cannot override it. In C# structs are implicitly sealed; therefore, they cannot be inherited. If we try to inherit from a sealed class in another class we will get compile time error about Inconsistent accessibility (code is shown in following code listing). In C# a method cannot be declared as sealed. However when we override a method in a derived class, we can declare the overridden method as sealed as shown below. By declaring it as sealed, we can avoid further overriding of this method.

Static Class

Static classes are classes that contain only static members. Following is an example of static class. public static MyClass { …..}

A class can be declared static, which indicates that it contains only static members. It is not possible to use the new keyword to create instances of a static class. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace that contains the class is loaded.

Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

Following are the main features of a static class:

·         They only contain static members.

·         They cannot be instantiated.

·         They are sealed.

·         They cannot contain Instance Constructors (C# Programming Guide).

Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.

Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor; however, they can have a static constructor. For more information, see Static Constructors (C# Programming Guide).

 

May 29, 2008

Difference between Namespace and Assembly in C# .Net

Filed under: Computer Science — Tags: , , — Md. Shaik Sadi @ 7:58 am

Assembly will contain Namespaces, Classes, Data types it’s a small unit of code for deployment. Assembly defines the name of the .dll file.
Namespace is used in order to avoid conflict of user defined classes

Namespace:
1) it is a Collection of names wherein each name is Unique.
2) They form the logical boundary for a Group of classes.
3) Namespace must be specified in Project-Properties.

Assembly:
1) It is an Output Unit.
2) It is a unit of Deployment & a unit of versioning.
3) Assemblies contain MSIL code.
4) Assemblies are Self-Describing. [e.g. metadata,manifest]
5)An assembly is the primary building block of a .NET Framework application.
6) It is a collection of functionality that is built, versioned, and deployed as a single implementation unit (as one or more files).
7) All managed types and resources are marked either as accessible only within their implementation unit, or by code outside that unit.

May 27, 2008

Simple Example of Inheritance and Interface (why do we use Inheritance and interface) in c# .Net

Filed under: Computer Science — Tags: , , — Md. Shaik Sadi @ 10:39 am
using System;
using System.Collections.Generic;
using System.Text;
interface Talkable
{
string Table();
}
class Animal { }
class Cat : Animal, Talkable
{
string Talkable.Table() { 
return "miao"; 
}
}
class Dog : Animal, Talkable
{
string Talkable.Table(){
return "bulk"; 
}
}
class Elephant : Animal
{
}
class MainClass
{
static void Main()
{
Animal[] AnimalArray = new Animal[3];
AnimalArray[0] = new Cat();
AnimalArray[1] = new Elephant();
AnimalArray[2] = new Dog();
foreach (Animal a in AnimalArray)
{
Talkable b = a as Talkable;
if (b != null)
Console.WriteLine("Baby
is called: {0}", b.Table());
}
}
}
output:
Baby is called: miao
Baby is called: bulk

Difference between class and struct in C# .Net

Filed under: Computer Science — Tags: , , — Md. Shaik Sadi @ 8:40 am

1. Classes are reference types and structs are value types.
Since classes are reference type, a class variable can be assigned null.But we cannot assign null to
a struct variable, since structs are value type.
2. When you instantiate a class, it will be allocated on the heap.When you instantiate a struct, it gets created on the stack.
3. You will always be dealing with reference to an object ( instance ) of a class. But you will not be dealing with references to an instance of a struct ( but dealing directly with them ).
4. When passing a class to a method, it is passed by reference. When passing a struct to a method, it’s passed by value instead of as a reference.
5. You cannot have instance Field initializers in structs.But classes can have
example:
class MyClass
{
int myVar =10; // no syntax error.
public void MyFun( )
{
// statements
}
}
struct MyStruct
{
int myVar = 10; // syntax error.
public void MyFun( )
{
// statements
}
}
6. Classes can have explicit parameterless constructors. But structs cannot have
7. Classes must be instantiated using the new operator. But structs can be
8. Classes support inheritance.But there is no inheritance for structs.
( structs don’t support inheritance polymorphism )
9. Since struct does not support inheritance, access modifier of a member of a struct cannot be protected or protected internal.11. A class is permitted to declare a destructor.But a struct is not
12. classes are used for complex and large set data. structs are simple to use.

May 14, 2008

Dynamic Method Invocation in C# .Net (Static Method and Non Static Method)

Filed under: Computer Science — Tags: , , — Md. Shaik Sadi @ 8:26 am

Create a solution in visual studio and add two project. First project name is  ”FirstProject” and second one is “DynamicInvoke”. In the first project add Class1.cs class and compile.

Class1.cs

using System;
public class Class1{
public static String method1()
{
return "I am Static method (method1) in class1";
}
public String method2()
{
return "I am a Instance Method (method2) in Class1";
}
public String method3(String s)
{
return "Hello " + s;
}
}

DynaInvoke.cs

using System;
using System.Reflection;
class DynamicInvoke
{
public static void Main(String[] args)
{
Assembly MyAssembly = Assembly.Load("FirstProject");
//For getting the assembly click the right mouse on the class1 oject then go to the property
Type MyType = MyAssembly.GetType("FirstProject.Class1");
//class name should be added with assembly like (assemblyName.className) (NameSpace with classname)
object MyObj = Activator.CreateInstance(MyType);

//Invoking a static method (How to invoke a static method??)
String str = (String)MyType.InvokeMember("method1", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, new object[] { });
Console.WriteLine(str);

//Invoking a non-static method (How to invoke a non static method??)
str = (String)MyType.InvokeMember("method2", BindingFlags.Default | BindingFlags.InvokeMethod, null, MyObj, new object[] { });
Console.WriteLine(str);

//Invoking a non-static method with parameters (How to invoke a non static method with parametres??)
object[] MyParameter = new object[] { "Sadi" };
str = (String)MyType.InvokeMember("method3", BindingFlags.Default | BindingFlags.InvokeMethod, null, MyObj, MyParameter);
Console.WriteLine(str);
}
}

In the second project add this code and run..

May 12, 2008

Tutorial On C#, Asp.Net, PHP, C++

Filed under: Computer Science — Tags: , , , , — Md. Shaik Sadi @ 10:22 am

If you want to expert in computer language ….

View the following link….

http://www.java2s.com/Tutorial/CSharp/CatalogCSharp.htm

Happy Coding Life

May 11, 2008

Amar Nei kono Tulona…

Filed under: Uncategorized — Md. Shaik Sadi @ 8:08 am

Sadi the Great

May 8, 2008

Difference between Abstract class and Interface in C# .Net

Filed under: Computer Science — Tags: , , — Md. Shaik Sadi @ 6:03 am

Interfaces are essentially having all method prototypes no definition but Abstract class can contain method definations also.

In short Interface is a abstract class having all methods abstract.

Both abstract classes and interfaces are used when there is a difference in behaviour among the sub-types extending the abstract class or implementing the interface.

When the sub-types behaviour is totally different then you use an interface, when the sub-types behaviour is partially common and different with respect to the supertype an abstract class is used. In an abstract class the partially common behaviour is given a concrete implementation. Since there is no common behaviour between an interface and a sub-type an interface does not have an implementation for any of its behaviour.

If you create a abstract class writing the abstract keyword in the declaration part then You only can inherit the class. You can not create an instance of this abstract class but can inherit the class and with creating the instance of the derived class you can access the method of the abstract class.

If you use a virtual keyword in a method then you can override this method in the subclass if you wish..

If you create a abstract method then you must override this method in the subclass other wise it shows error in the program.

April 16, 2008

what’s the difference between architecture and framework? in C# .Net

Filed under: Computer Science — Tags: , , — Md. Shaik Sadi @ 9:20 am

as far as i know, the architecture is the first design result which
should consider some functions and properties of quality,sometimes
environment of the software,while framework is focus on domain-specificated
application which is something general to a certain domain.

could i have such a conclusion,if i do something first,it’s
architecture,if i do it again it called repeated labor,if i do it thirdly
then i could consider to make it a common framework?

Alas, both words are heavily overloaded in the software industry.
Basically the definition depends on which tool vendor one is talking to.
for example, the Computer Desktop Encyclopedia defines the terms as:

architecture: The set of protocols for communicating with another
application.

framework: the set of building blocks for constructing an application.

I don’t like either of those definitions because they don’t agree with
what Daddy told me many moons ago as I sat on his knee. FWIW, I prefer
the definitions:

architecture: The strategy and/or infrastructure for accomplishing some
specific goal that defines or supports cooperation among multiple
distinct software elements.

framework: An infrastructure that acts as a generic skeleton for
combining customized software elements. Generally a framework can be
reused for multiple applications or systems of applications.

Since both can be infrastructures, in my view the distinction is
primarily about reuse. Architectures are usually focused on specific
problem contexts while frameworks are designed to be used in entirely
different problem contexts.

March 20, 2008

This Article explain What is the difference between DROP,DELETE and TRUNCATE

Filed under: Computer Science, Database — Tags: — Md. Shaik Sadi @ 8:39 am

The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to
COMMIT or ROLLBACK the transaction to make the change permanent or to undo it.

TRUNCATE removes all rows from a table. The operation cannot be rolled back. As such, TRUCATE is faster and doesn’t use as much undo space as a DELETE.

The DROP command removes a table from the database. All the tables’ rows,
indexes and privileges will also be removed. The operation cannot be rolled back.

DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. Therefore DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.

« Newer PostsOlder Posts »

Blog at WordPress.com.